RawRabbit 使用或不使用 INamingConvention 设置队列名称
RawRabbit Set queue name with or without INamingConvention
在 C# 中使用 RawRabbit 时如何指定队列名称?我在任何地方都找不到关于如何做的例子。如果实施 INamingConvention 是唯一的方法,那么如何使用 INamingConvention 来实现?
我已经尝试按照以下方式指定队列名称,但它仍然使用 _appname 作为后缀。
client.SubscribeAsync<string>(async (msg, context) =>
{
Console.WriteLine("Recieved: {0}", msg);
}, cfg=>cfg.WithQueue(q=>q.WithName("ha.test")));
刚刚阅读了 GitHub 中 RawRabbit 的源代码。看起来您可以使用 WithSubscriberId(string subscriberId)。该 subscriberId 设置名称后缀,该后缀附加到您设置的队列名称的末尾。
队列是使用 QueueConfiguration class
中的 FullQueueName 属性 创建的
public string FullQueueName
{
get
{
var fullQueueName = string.IsNullOrEmpty(NameSuffix)
? QueueName
: $"{QueueName}_{NameSuffix}";
return fullQueueName.Length > 254
? string.Concat("...", fullQueueName.Substring(fullQueueName.Length - 250))
: fullQueueName;
}
}
所以只需将 subscriberId 设置为空字符串即可。
client.SubscribeAsync<string>(async (msg, context) =>
{
Console.WriteLine("Recieved: {0}", msg);
}, cfg=>cfg.WithQueue(q=>q.WithName("ha.test")).WithSubscriberId(""));
类似的东西。我的电脑上没有 .NET Core,所以我无法验证它,所以尽管告诉我它不起作用。
已更新
根据 NewToSO 的评论
修复了代码
在 C# 中使用 RawRabbit 时如何指定队列名称?我在任何地方都找不到关于如何做的例子。如果实施 INamingConvention 是唯一的方法,那么如何使用 INamingConvention 来实现?
我已经尝试按照以下方式指定队列名称,但它仍然使用 _appname 作为后缀。
client.SubscribeAsync<string>(async (msg, context) =>
{
Console.WriteLine("Recieved: {0}", msg);
}, cfg=>cfg.WithQueue(q=>q.WithName("ha.test")));
刚刚阅读了 GitHub 中 RawRabbit 的源代码。看起来您可以使用 WithSubscriberId(string subscriberId)。该 subscriberId 设置名称后缀,该后缀附加到您设置的队列名称的末尾。
队列是使用 QueueConfiguration class
中的 FullQueueName 属性 创建的public string FullQueueName
{
get
{
var fullQueueName = string.IsNullOrEmpty(NameSuffix)
? QueueName
: $"{QueueName}_{NameSuffix}";
return fullQueueName.Length > 254
? string.Concat("...", fullQueueName.Substring(fullQueueName.Length - 250))
: fullQueueName;
}
}
所以只需将 subscriberId 设置为空字符串即可。
client.SubscribeAsync<string>(async (msg, context) =>
{
Console.WriteLine("Recieved: {0}", msg);
}, cfg=>cfg.WithQueue(q=>q.WithName("ha.test")).WithSubscriberId(""));
类似的东西。我的电脑上没有 .NET Core,所以我无法验证它,所以尽管告诉我它不起作用。
已更新 根据 NewToSO 的评论
修复了代码