4

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

knightpfhor
  • 9,299
  • 3
  • 29
  • 42
Bitsian
  • 2,238
  • 5
  • 37
  • 72

3 Answers3

6

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>();
         ...
     }
}
Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65
  • Thank you!! That works, its asynchronous, but I dont know why , my VS got hung and crashed thrice when i sent any messages to the service bus. I have updated the question and added some code please look at it and let me know if you know what the problem is. Thanks again – Bitsian Feb 13 '13 at 13:11
  • The BeginReceive method returns immediately, meaning your while loop is cycling over and over again (infinite loop). A while loop like this one works fine with synchronous methods, but it doesn't when you use asynchronous calls – Sandrino Di Mattia Feb 13 '13 at 14:18
  • Thanks i fixed it by putting some conditional statements in the while loop – Bitsian Feb 14 '13 at 04:58
1

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.

EZDsIt
  • 921
  • 1
  • 9
  • 14
1

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();
}
Thomas
  • 24,234
  • 6
  • 81
  • 125
  • I'm not sure why they use the synchronous `Receive()` in the getting started [tutorial](https://azure.microsoft.com/en-us/documentation/articles/service-bus-brokered-tutorial-dotnet/). When I tried to change that to await `ReceiveAsync` it stopped receiving any messages! – orad May 28 '16 at 22:56
  • Nevermind, used `Task.WaitAll(Queue());` in the main method and it worked! – orad May 29 '16 at 00:30
  • It just that the tutorial is not up to date. Anyway it is not always a good thing to use async. Just use async when you need it. What was wrong with your code ? – Thomas May 29 '16 at 08:53
  • Issue in my code was that it was calling the async `Queue()` in the main method without `Task.WaitAll`. In the case of this tutorial I think async is preferred because otherwise you have to user `Thread.Sleep` in a loop like you see in the tutorial. – orad May 29 '16 at 14:29
  • @orad, the `Thread.Sleep` is only here to simulate the message processing – Thomas May 29 '16 at 20:57
  • OK, I'll send a PR and keep `Thread.Sleep` with a comment about that. Thanks. – orad May 30 '16 at 03:00