Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop receiving messages from SubscriptionClient

How do you stop receiving messages from a subscription client set as a an event-driven message pump? I currently have some code that works however when I run two tests consecutively they second breaks. I'm fairly sure messages are still being pulled off the subscription from the first instance i created.

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);
like image 912
Tom Squires Avatar asked Jun 10 '13 13:06

Tom Squires


People also ask

What is a service bus subscription?

Azure Service Bus is a fully managed enterprise message broker with message queues and publish-subscribe topics (in a namespace). Service Bus is used to decouple applications and services from each other, providing the following benefits: Load-balancing work across competing workers.

How do I subscribe to Azure Service Bus?

Get the Service Bus NuGet package To install the NuGet package in your application, do the following: In Solution Explorer, right-click References, then click Manage NuGet Packages. Search for "Service Bus" and select the Microsoft Azure Service Bus item.


1 Answers

You need to do two things.

First, call subscriptionClient.Close(), which will eventually (but not immediately) stop the message pump.

Second, on your message received callback, check if the client is closed, like so:

if (subscriptionClient.IsClosed)
    return;
like image 127
Ekevoo Avatar answered Oct 14 '22 14:10

Ekevoo