Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous method for QueueClient.Receive()?

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

like image 435
Bitsian Avatar asked Feb 17 '23 13:02

Bitsian


1 Answers

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>();
         ...
     }
}
like image 154
Sandrino Di Mattia Avatar answered Feb 22 '23 02:02

Sandrino Di Mattia