本文介绍了对于QueueClient.Receive异步方法()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的服务总线来连接Web角色和辅助角色。我的工作者角色是一个连续的循环,我收到通过与QueueClient.Receive()方法Web角色发送的消息。

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?

我发现从和我的MSDN文档这种方法BeginReceive希望这将是回答我的问题,但我不知道如何使用此方法。该方法的参数异步回调和我不对象的状态知道它们是什么。

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.

任何想法?

更新:
这要感谢Sandrino一个很好的解决方案,它的工作原理asynchrnously。不过是异步的,现在给我一些问题。我的VS崩溃。不知道是什么问题。下面是code正在使用。

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角色:

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

我是错过了什么这是在工作和崩溃使得VS?由于移出到BeginReceive,当IW与使用QueueClient.Receive它是工作的罚款,而不是崩溃了。

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.

感谢

推荐答案

BeginReceive 方法是在你的案件的路要走。您通常会这样称呼它:

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

这篇关于对于QueueClient.Receive异步方法()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 11:18