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
source share