Question

I call BeginReceive() on a SubscriptionClient like this:

client.BeginReceive(new TimeSpan(6, 0, 0), new AsyncCallback(DownloadResults), null);

And in the DownloadResults() callback I call client.EndReceive() [client is a static member of the class]

static public void DownloadResults(IAsyncResult ar)
{
    // Get the message from the service bus.
    msg = client.EndReceive(ar);
    if (msg == null)
    {
        Console.WriteLine("Error!");
        return;
    }
}

Sometimes client.EndReceive() returns null. It's not very clear to me by looking at the MSDN page for SubscriptionClient.EndReceive() as to exactly when EndReceive() returns null. Is it because the timelimit I specified in BeginReceive() was exceeded? Or is it because there was an error?

Was it helpful?

Solution

It appears that if the time limit is exceeded, the callback is called. When we try to retrieve the message by calling SubscriptionClient.EndReceive(), null will be returned.

The default timeout value is one minute, and there is no way to set an infinite timeout. If you want an infinite timeout, you can call SubscriptionClient.BeginReceive() again if the message is null.

For example:

static public void DownloadResults(IAsyncResult ar)
{
    // Get the message from the service bus.
    msg = client.EndReceive(ar);
    if (msg == null)
    {
        // Start waiting for the message again.
        client.BeginReceive(new TimeSpan(6, 0, 0), new AsyncCallback(DownloadResults), null);
        return;
    }
}

Source: http://social.msdn.microsoft.com/Forums/windowsazure/en-US/b14587ce-3fc7-4b56-8a35-4f2c6babce26/servicebus-subscriptionclientendreceive-returns-null

OTHER TIPS

The contract for Receive states that ServiceBus will not throw an Exception. When Receive/BeginReceive/ReceiveAsync times out, the call will do whatever it needs to do to return a value (such as execute your callback) and you will get back a null in a timeout situation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top