【问题标题】:Azure Servicebus messageid is always nullAzure Servicebus messageid 始终为空
【发布时间】:2017-06-14 19:09:23
【问题描述】:

我有一个相当简单的函数,可以将消息发送到 azure servicebus 队列。

public async Task<string> SendMessagesToQueue(string serviceBusConnectionString, int message, string queueName)
    {
        try
        {
            QueueClient queueClient = new QueueClient(serviceBusConnectionString, queueName, ReceiveMode.PeekLock);
            // Create a new brokered message to send to the queue
            var brokeredMessage = new Message($"Message {message}");

            // Send the message to the queue
            await queueClient.SendAsync(brokeredMessage);
            await queueClient.CloseAsync();
            // Delay by 10 milliseconds so that the console can keep up
            await Task.Delay(10);
            return brokeredMessage.MessageId;
        }
        catch (Exception ex)
        {
            return ex.ToString();
            throw;
        }
    }

这可以正常工作并将消息放入队列中。但是,bro​​keredMessage.MessageId 始终为空。

是否有正确/更好的方法来检索消息 ID?

【问题讨论】:

    标签: azure azureservicebus


    【解决方案1】:

    MessageId 是 Brokered 消息的一个属性,可用于在消息接收期间识别队列中的消息。对于队列中的消息,messageId 不必是唯一的,除非队列启用了 requireduplicateDetection 属性,这可能会导致丢失的消息。 因此,如果您发送没有 messageId 的消息,它将自动生成类似于序列号。

    【讨论】:

      【解决方案2】:

      MessageId 是一个属性,您可以使用 Message 类中的公共 setMessageId 函数进行设置。

      在最新版本的 Java 客户端中,即使您没有设置它,当您收到消息时也会返回消息 ID。如果您在发送消息之前设置它,您将在接收该消息时返回与消息 ID 相同的值。

      【讨论】:

        【解决方案3】:

        我猜是因为MessageId 是用户定义的值。您可以随意生成和分配它。 Brokered​Message.​Message​Id Property

        【讨论】:

        • 很公平。有没有办法获取队列中消息的 ID?喜欢id=b192187a-52a9-45a6-ba6f-c4b32ec9eb45
        • 如果我对您的理解正确,您只需要接收消息并从中获取MessageId。例如,您可以像 here 那样轻松地做到这一点。
        • 你理解我的正确。但是,这不是 Microsoft.Azure.Management.ServiceBus 库中提供的功能
        • 你说的是this库吗?为什么不使用default one
        • @w2olves message.MessageId 可用于两个库。将来,将支持插件,其中一个插件将允许您注册消息 ID 工厂。
        【解决方案4】:

        MessageId 是 Brokered Message 的一个属性。 Brokered Message 有两个主要属性系统属性和自定义属性。而 MessageId 是自定义属性之一。您可以在消息提交期间设置 MessageId,也可以对其进行编辑并重新提交到另一个队列/主题。它没有任何特定的格式。它可以是字符串、guid、整数。此外,MessageId 不必是唯一的,而 SequenceNumber 是队列中消息唯一的。

        您可以使用 WindowsAzure.ServiceBus 或 Microsoft.Azure.ServiceBus 库来执行消息传递操作。其中 WindowsAzure.ServiceBus 不支持 .Net Core 应用程序,但 Microsoft.Azure.ServiceBus 将支持

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-05
          • 2017-07-31
          • 2019-07-18
          • 2019-08-18
          相关资源
          最近更新 更多