7

您如何停止从订阅客户端接收消息,该订阅客户端设置为事件驱动的消息泵?我目前有一些有效的代码,但是当我连续运行两个测试时,它们会第二次中断。我相当肯定消息仍然从我创建的第一个实例的订阅中提取。

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);
4

3 回答 3

6

你需要做两件事。

首先, call subscriptionClient.Close(),这将最终(但不是立即)停止消息泵。

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

if (subscriptionClient.IsClosed)
    return;
于 2016-07-28T14:26:30.183 回答
3

您可以调用SubscriptionClient.Close()以停止处理进一步的消息。

在代码的注释中也指出:

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

于 2013-06-10T22:35:00.390 回答
3

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

于 2015-08-06T14:55:42.153 回答