简体   繁体   English

为Microsoft Azure Service Bus代理消息实现SendBatchAsync

[英]Implementing SendBatchAsync for Microsoft Azure Service Bus Brokered Messages

My requirement is that I have to send a list of brokered messages to the azure service bus asynchronously. 我的要求是我必须异步将代理消息列表发送到azure服务总线。 However I am not able to implement the SendBatchAsync method properly. 但是,我无法正确实现SendBatchAsync方法。 Let me explain in detail. 让我详细解释。 Here is my code: 这是我的代码:

public async Task SendBatchEnrollmentMessages()
{
    while(somevalue) 
    {
        //logic to fetch data from the SQL db through stored proc into list of brokered message i.e. messageList
        if(messageList.Count() > 0)
        {
            await sender.SendMessagesAsync(messageList);
        } 
        //some more logic for somevalue
    }
}

where the SendMessageAsync logic is : SendMessageAsync逻辑为:

public async Task SendMessagesAsync(List<BrokeredMessage> brokeredMessageList)
{
    var topicClient = CreateTopicClient();
    await topicClient.SendBatchAsync(brokeredMessageList);
}

My issue is that when I debug the application using break point, the compiler comes till await topicClient.SendBatchAsync(brokeredMessageList); 我的问题是,当我使用断点调试应用程序时,编译器会一直await topicClient.SendBatchAsync(brokeredMessageList); and exits the code ie application debuggin is completed. 并退出代码,即应用程序调试已完成。 It doesn't return back to the while condition. 它不会返回到while条件。 However instead of using SendBatchAsync if I use SendBatch , it works fine. 但是,而是如果我使用的sendBatch使用SendBatchAsync的,它工作正常。 What am I doing wrong? 我究竟做错了什么?

Solution: The issue was with the test method which was calling the above funciton. 解决方案:问题在于调用上述函数的测试方法。 It was of type void. 它的类型为void。 It should have been of type async Task. 它应该是异步任务类型。 A big thanks to Ned Stoyanov for helping me out in this. 非常感谢Ned Stoyanov在此方面的帮助。

An async method returns when after encounters an await statement and sets up the asynchronous operation being awaited. 当遇到await语句并设置正在等待的异步操作时, async方法将返回。 The rest of the method then continues after the await is finished. 然后, await完成后,其余方法将继续。

You probably can't step through async methods like that, but try putting a breakpoint after the await and it should get hit when the asynchronous call completes. 您可能无法逐步执行类似的async方法,但是请尝试在await之后放置一个断点,并且在异步调用完成后应该会遇到断点。

Alternatively you may have a deadlock, see this post for some ways to avoid it. 否则,您可能会陷入僵局,请参阅这篇文章以了解避免这种情况一些方法。

As mentioned in our discussion the unit test needs to be async as well, return a Task and await any async calls 正如我们在讨论中提到的,单元测试也需要async ,返回Taskawait任何async调用

[TestMethod] 
public async Task SendRegionsEnrollmentMessages() 
{ 
    EventManager eventMgr = new EventManager(clientUrn, programUrn, "CW"); 
    await eventMgr.SendBatchEvents(EventType.ENROLLMENT); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Azure函数将多个代理消息输出到Azure服务总线主题 - Output multiple Brokered Messages to an Azure Service Bus Topic(s) with Azure Functions SubscriptionClient.RegisterMessageHandler() 是否在 Azure 服务总线中使用“代理连接”? - Is SubscriptionClient.RegisterMessageHandler() Using 'Brokered Connections' in Azure Service Bus? 如何处理不带锁定令牌的Azure服务总线代理消息? - How to process azure service bus brokered message without lock token? 使用代理消息在门户中测试Azure功能服务总线触发器 - Testing an Azure Function Service Bus Trigger in the Portal with a Brokered Message Azure 服务总线中的消息顺序 - Order of messages in Azure Service Bus 是否有人为Windows Azure Service Bus Brokered Messaging服务创建了FTP适配器? - Has anyone created an FTP adapter for the Windows Azure Service Bus Brokered Messaging service? 使用Azure Service Bus中继消息推送消息 - Push messages with Azure Service Bus relayed messages Microsoft服务总线-使用OnMessage()方法从总线接收消息 - Microsoft service bus - Receive messages from Bus with OnMessage() method Azure服务总线:忽略我发送的消息 - Azure Service Bus: ignoring messages sent by me Azure Service Bus - 使用OnMessage()方法接收消息 - Azure Service Bus - Receive Messages with OnMessage() Method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM