0

My requirement is that I have to send a list of brokered messages to the azure service bus asynchronously. However I am not able to implement the SendBatchAsync method properly. 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 :

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); and exits the code i.e application debuggin is completed. It doesn't return back to the while condition. However instead of using SendBatchAsync if I use SendBatch, it works fine. What am I doing wrong?

Solution: The issue was with the test method which was calling the above funciton. It was of type void. It should have been of type async Task. A big thanks to Ned Stoyanov for helping me out in this.

nitinvertigo
  • 1,180
  • 4
  • 32
  • 56

1 Answers1

1

An async method returns when after encounters an await statement and sets up the asynchronous operation being awaited. The rest of the method then continues after the await is finished.

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.

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

[TestMethod] 
public async Task SendRegionsEnrollmentMessages() 
{ 
    EventManager eventMgr = new EventManager(clientUrn, programUrn, "CW"); 
    await eventMgr.SendBatchEvents(EventType.ENROLLMENT); 
}
NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
  • Pls see my update in the question. As you rightly suggested this has to be due to await operator. However I need it to return to the while condition because I have to send more brokered messages as batches. But that is not happening. The code execution just stops. Is there a way I can accomplish this? – nitinvertigo Jun 03 '15 at 11:22
  • is there any exceptions being thrown? Or maybe `messageList.Count()` is 0? – NeddySpaghetti Jun 03 '15 at 11:25
  • No exception is thrown and the count is also not 0. When the compiler comes to that line, the code execution just stops i.e. it does not even go to the } at the end.. – nitinvertigo Jun 03 '15 at 11:30
  • the code you posted looks good, but maybe you have a deadlock, try replacing `await topicClient.SendBatchAsync(brokeredMessageList)` with `await topicClient.SendBatchAsync(brokeredMessageList).ConfigureAwait(false)` – NeddySpaghetti Jun 03 '15 at 11:32
  • Same thing is happening i.e. the compiler is still coming to that line and exiting. – nitinvertigo Jun 03 '15 at 11:38
  • Try changing the line `await topicClient.SendBatchAsync(brokeredMessageList);` to `await Task.Delay(5000);` and see what happens – NeddySpaghetti Jun 03 '15 at 11:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79527/discussion-between-nitinvertigo-and-ned-stoyanov). – nitinvertigo Jun 03 '15 at 11:44