停止从SubscriptionClient接收消息

Tom*_*res 6 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);
Run Code Online (Sandbox Code Playgroud)

Eke*_*voo 5

你需要做两件事.

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

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

if (subscriptionClient.IsClosed)
    return;
Run Code Online (Sandbox Code Playgroud)


Abh*_*Lal 3

您可以致电SubscriptionClient.Close()以停止处理更多消息。

代码中的注释中也注明了:

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

  • -1 那不起作用。最后一条消息仍然通过 OnMessage 回调发出,并在接收方方法尝试完成它时引发异常。 (2认同)