Azure ServiceBus订阅客户端.EndReceive()返回null

本文关键字:返回 null EndReceive 客户端 ServiceBus Azure | 更新日期: 2023-09-27 18:00:09

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

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

在DownloadResults()回调中,我调用客户端。EndReceive()[客户端是类的静态成员]

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

有时是客户。EndReceive()返回null。通过查看SubscriptionClient的MSDN页面,我不太清楚。EndReceive()返回null的确切时间。是因为超过了我在BeginReceive()中指定的时间限制吗?还是因为出现了错误?

Azure ServiceBus订阅客户端.EndReceive()返回null

如果超过时间限制,则会调用回调。当我们试图通过调用SubscriptionClient.EndReceive()来检索消息时,将返回null

默认超时值为一分钟,并且无法设置无限超时。如果您想要无限超时,如果消息是null,则可以再次调用SubscriptionClient.BeginReceive()

例如:

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

Receive的约定声明ServiceBus不会抛出异常。当Receive/BeginReceive/ReceAsync超时时,调用将执行它需要做的任何事情来返回值(例如执行回调),并且在超时情况下您将返回null。