[英]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
,返回Task
并await
任何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.