Azure ServiceBus SubscriptionClient.EndReceive()返回null

时间:2014-04-25 09:09:12

标签: c# azure servicebus

我在SubscriptionClient上调用BeginReceive(),如下所示:

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

在DownloadResults()回调中,我调用client.EndReceive()[client是该类的静态成员]

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

有时,client.EndReceive()会返回null。通过查看SubscriptionClient.EndReceive()的MSDN页面来确定EndReceive()返回null的确切时间并不是很清楚。是因为我在BeginReceive()中指定的时间限制被超出了吗?或者是因为有错误?

2 个答案:

答案 0 :(得分:1)

如果超出时间限制,则会调用回调。当我们通过调用SubscriptionClient.EndReceive()尝试检索邮件时,将返回null

默认超时值是一分钟,无法设置无限超时。如果您想要无限超时,如果邮件为SubscriptionClient.BeginReceive(),则可以再次致电null

例如:

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;
    }
}

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

答案 1 :(得分:0)

Receive的合同规定ServiceBus不会抛出异常。当Receive / BeginReceive / ReceiveAsync超时时,调用将执行返回值所需的任何操作(例如执行回调),并在超时情况下返回null。