停止从SubscriptionClient接收消息

时间:2013-06-10 13:42:03

标签: c# azure azureservicebus

如何停止接收来自订阅客户端设置为事件驱动的消息泵的消息?我目前有一些代码可以工作,但是当我连续运行两个测试时,他们第二次休息。我很确定消息仍然是从我创建的第一个实例中删除订阅。

http://msdn.microsoft.com/en-us/library/dn130336.aspx

OnMessageOptions options = new OnMessageOptions();
            options.AutoComplete = true; // Indicates if the message-pump should call complete on messages after the callback has completed processing.

            options.MaxConcurrentCalls = 1; // Indicates the maximum number of concurrent calls to the callback the pump should initiate 

            options.ExceptionReceived += LogErrors; // Enables you to be notified of any errors encountered by the message pump

            // Start receiveing messages
            Client.OnMessage((receivedMessage) => // Initiates the message pump and callback is invoked for each message that is received. Calling Close() on the client will stop the pump.

                {
                    // Process the message
                    Trace.WriteLine("Processing", receivedMessage.SequenceNumber.ToString());
                }, options);

3 个答案:

答案 0 :(得分:5)

你需要做两件事。

首先,调用subscriptionClient.Close(),最终(但不会立即)停止消息泵。

其次,在您的消息收到回调时,检查客户端是否已关闭,如下所示:

if (subscriptionClient.IsClosed)
    return;

答案 1 :(得分:2)

我看了看并完全相同的行为。即使您关闭订阅客户端,消息泵也不会停止尝试处理消息。如果您的消息处理处理程序尝试对消息执行.Complete.Abandon,则会抛出异常,因为客户端已关闭。需要一种方法来阻止泵人员。

答案 2 :(得分:0)

您可以致电SubscriptionClient.Close()以停止处理其他邮件。

在代码中的注释中也表示:

  

在客户端上调用Close()将停止泵。

相关问题