Asynchronous method for QueueClient.Receive()?

蓝咒 提交于 2019-12-10 16:52:27

问题


I am using service bus to connect web role and the worker role. My worker role is in a continuous loop and i am receiving the message sent by web role with the QueueClient.Receive() method.

But with this method, if there is no message on the service bus queue, it waits for a few seconds to receive the message rather than moving to the next line for further execution. I was hoping there would be some asynchronous method for receiving messages? or at least some way for setting this wait time?

I found this BeginReceive method from msdn documentation of QueueClient and i was hoping that this would be the answer to my question but i dont know how to use this method. The method parameters are async callback and object state which i dont know what they are.

Any ideas?

UPDATE: Thanks to a great solution by Sandrino, it works asynchrnously. But being asynchronous is now giving me some problems. My VS is crashing. Not sure what the problem is. Below is the code am using.

Worker Role:

public override void Run()
    {
while (!IsStopped)
        {                
                // Receive the message from Web Role to upload the broadcast to queue
                BroadcastClient.BeginReceive(OnWebRoleMessageReceived, null);                    

                // Receive the message from SignalR BroadcastHub
                SignalRClient.BeginReceive(OnSignalRMessageReceived, null);                    

            }
}


public void OnWebRoleMessageReceived(IAsyncResult iar)
    {
        BrokeredMessage receivedBroadcastMessage = null;
        receivedBroadcastMessage = BroadcastClient.EndReceive(iar);

        if (receivedBroadcastMessage != null)
        {   //process message
           receivedBroadcastMessage.Complete();
        }
    }

public void OnSignalRMessageReceived(IAsyncResult iar)
    {
        BrokeredMessage receivedSignalRMessage = null;
        receivedSignalRMessage = SignalRClient.EndReceive(iar);

        if (receivedSignalRMessage != null)
        {
            //process message
           receivedSignalRMessage.Complete();

           WorkerRoleClient.Send(signalRMessage);
        }
     }

Am i missing out on anything which is making the VS over work and crash? Because before shifting out to BeginReceive, when iw as using QueueClient.Receive it was working fine and not crashing.

Thanks


回答1:


The BeginReceive method is the way to go in your case. You would typically call it like this:

void SomeMethod() 
{
     ...
     client.BeginReceive(TimeSpan.FromMinutes(5), OnMessageReceived, null);
     ...
}

void OnMessageReceived(IAsyncResult iar)
{
     var msg = client.EndReceive(iar);
     if (msg != null)
     {
         var body = msg.GetBody<MyMessageType>();
         ...
     }
}



回答2:


This is the way I did it (extending Sandrino De Mattia's solution):

void SomeMethod() 
{
    ...
    client.BeginReceive(TimeSpan.FromSeconds(5), OnMessageReceived, null);
    ...
}

void OnMessageReceived(IAsyncResult iar)
{
    if(!IsStopped)
    {
        var msg = client.EndReceive(iar);
        if (msg != null)
        {
            var body = msg.GetBody<MyMessageType>();

            ... //Do something interesting with the message

            //Remove the message from the queue
            msg.Complete();

            client.BeginReceive(TimeSpan.FromSeconds(5), OnMessageReceived, null);
        }
    }
}

That way I have an "endless loop" with a stopping mechanism.




回答3:


The latest version of the Azure ServiceBus SDK (download link) provides full support to receive messages asynchronously :

async Task TestMethod()
{
    string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

    QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "TestQueue");
    var message = await Client.ReceiveAsync();
}


来源:https://stackoverflow.com/questions/14852670/asynchronous-method-for-queueclient-receive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!