Asynchronous method for QueueClient.Receive ()?

I use the service bus to connect the web role and the working role. My work role is in a continuous loop, and I get a message sent by the web role using the QueueClient.Receive () method.

But with this method, if there is no message in the service bus queue, it waits a few seconds to receive the message, and not go to the next line for further execution. I was hoping there would be some kind of asynchronous method for receiving messages? or at least in some way to set this timeout?

I found this BeginReceive method from the msdn QueueClient documentation, and I was hoping this would be the answer to my question, but I don't know how to use this method. Method parameters are an asynchronous callback and state of the object, which I do not know what they are.

Any ideas?

UPDATE: Thanks to Sandrino's excellent solution, it works asynchronously. But being asynchronous now gives me some problems. My VS crashed. I don’t know what the problem is. Below is the am code.

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 everything that makes VS work and crash? Because before switching to BeginReceive, when iw uses QueueClient.Receive, it works fine and doesn't crash.

thanks

+4
source share
3 answers

The BeginReceive method is the way to go in your case. You usually 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>(); ... } } 
+6
source

So I did this (Sandrino De Mattia solution extension):

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

So I have an "infinite loop" with a stop mechanism.

+1
source

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

 async Task TestMethod() { string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString"); QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "TestQueue"); var message = await Client.ReceiveAsync(); } 
+1
source

All Articles