服务总线在ReceiveAndDelete模式下成功完成后,将重新启动消息处理

时间:2019-03-08 11:16:11

标签: c# azure azureservicebus azure-servicebus-queues

我正在ReceiveAndDeleteMode中接收消息,成功处理完消息后,消息处理将重新开始。

该消息的TTL为14小时,我的代码如下:

    private static async Task ProcessMessagesAsync(Message message, CancellationToken token)
    {
       bool result;
       try
       {
         IQueueClient queueClient = new QueueClient(serviceBusConnectionString, serviceBusQueueName, ReceiveMode.ReceiveAndDelete);

         var receivedMessageTrasactionId = Convert.ToInt64(Encoding.UTF8.GetString(message.Body));

         result = await DataCleanse.PerformDataCleanse(receivedMessageTrasactionId);                                                           
        }
        catch (Exception ex)
        {
          Log4NetErrorLogger(ex);
          throw ex;
        }

    }

这是我拥有的注册方法:

private static void RegisterOnMessageHandlerAndReceiveMessages()
        {
            IQueueClient queueClient = new QueueClient(serviceBusConnectionString, serviceBusQueueName);

            // Configure the MessageHandler Options in terms of exception handling, number of concurrent messages to deliver etc.
            var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
            {
                // Maximum number of Concurrent calls to the callback `ProcessMessagesAsync`, set to 1 for simplicity.
                // Set it according to how many messages the application wants to process in parallel.
                MaxConcurrentCalls = 1,

                // Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
                // False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
                AutoComplete = true
            };

            // Register the function that will process messages
            queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
        }

请告知我在哪里做错了。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您正在初始化两个QueueClient实例,并且其中只有一个具有ReceiveMode.ReceiveAndDelete标志。从您提供的代码中,看起来好像没有使用带有标志的代码。因此,实际接收邮件的客户端处于默认的“查看/锁定”模式。您应该能够从ProcessMessagesAsync删除客户端,并更新RegisterOnMessageHandlerAndReceiveMessages以使用接收和删除功能。

private static void RegisterOnMessageHandlerAndReceiveMessages()
{
    IQueueClient queueClient = new QueueClient(serviceBusConnectionString, serviceBusQueueName);
    ///....
}

然后

private static async Task ProcessMessagesAsync(Message message, CancellationToken token)
{
   bool result;
   try
   {
     var receivedMessageTrasactionId = Convert.ToInt64(Encoding.UTF8.GetString(message.Body));

     result = await DataCleanse.PerformDataCleanse(receivedMessageTrasactionId);                                                           
    }
    catch (Exception ex)
    {
      Log4NetErrorLogger(ex);
      throw ex;
    }

}