【问题标题】:RawRabbit Set queue name with or without INamingConventionRawRabbit 使用或不使用 INamingConvention 设置队列名称
【发布时间】:2017-09-17 05:03:29
【问题描述】:

在 C# 中使用 RawRabbit 时如何指定队列名称?我在任何地方都找不到如何做到这一点的例子。如果实现 INamingConvention 是唯一的方法,那么如何使用 INamingConvention 来实现呢?

我已尝试按照以下方式指定队列名称,但它仍然使用 _appname 作为后缀。

client.SubscribeAsync<string>(async (msg, context) =>
        {
          Console.WriteLine("Recieved: {0}", msg);
        }, cfg=>cfg.WithQueue(q=>q.WithName("ha.test")));

【问题讨论】:

    标签: c# rabbitmq rawrabbit


    【解决方案1】:

    只是在 GitHub 上阅读 RawRabbit 的源代码。看起来有一个 WithSubscriberId(stringsubscriberId) 可供您使用。该subscriberId 设置了附加到您设置的队列名称末尾的名称后缀。

    队列是使用 QueueConfiguration 类中的 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(""));
    

    类似的东西。我的 PC 上没有 .NET Core,所以我无法验证它,所以请随时告诉我它不起作用。

    已更新 根据 NewToSO 的评论修复了代码

    【讨论】:

    • 它只需一次更正即可。 .WithSubscriberId("") 需要附加到 cfg.WithQueue 而不是 q.WithName。谢谢。
    猜你喜欢
    • 2018-08-28
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多