1

I'm pretty confused as too which method is going to be the best solution for me.

I have a controller that calls a method 'PostEventEmailMessage' and inside the method I call

// do some work
var queueClient = QueueClient.CreateFromConnectionString(serviceBusConnectionString, queueName);
var sequenceToCancel = queueClient.ScheduleMessageAsync(message, postEventTimeToSendEmail);
queueClient.CloseAsync(); 
// do remaining work

where I'm really confused is which method below is the best to make it run synchronously or if there is a better solution all together in Asp.Net MVC? It seems like the first 2 options have deadlock issues, according to the links I'm referencing.

Calling async method synchronously

How would I run an async Task<T> method synchronously?

Should I use 1.

var sequenceToCancel = queueClient.ScheduleMessageAsync(message, postEventTimeToSendEmail).GetAwaiter().GetResult();

2.

var sequenceToCancel = queueClient.ScheduleMessageAsync(message, postEventTimeToSendEmail).Result;

3.

var sequenceToCancel = Task.Run(() => queueClient.ScheduleMessageAsync(message, postEventTimeToSendEmail)).Result;

my controller looks something like this

public ActionResult SaveSpaceEvent(Details details) {
    _spaceservice.SaveSpaceEvent(details);
}

then the 'SaveSpaceEvent' method looks like this

public void SaveSpaceEvent(Details, details) {
    // do some stuff
    _messageService.PostEventEmailMessage(details)
    // do more stuff
}

and the async queue message is being called in 'PostEventEmailMessage'

chuckd
  • 13,460
  • 29
  • 152
  • 331
  • You should read [Stephen Cleary‘s blog](https://blog.stephencleary.com/) ;o) – Sir Rufo Dec 20 '17 at 07:56
  • can you recommend a specific post he made, there are lots! – chuckd Dec 20 '17 at 08:04
  • All async blog articles are worth reading, but you may start with [Don't Block on Async Code](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html) first – Sir Rufo Dec 20 '17 at 08:15

1 Answers1

1

Why do you ever want to call this method synchronously? Make your controller action async and call method with await:

public async Task<ActionResult> SomeAction()
{
    // ...
    var sequenceToCancel = await queueClient.ScheduleMessageAsync(message, postEventTimeToSendEmail);
    // ...
}
CodeFuller
  • 30,317
  • 3
  • 63
  • 79
  • hi CodeFuller. I don't know if this makes sense, but I need to respond to the user with a message in a modal window. I can't respond until everything is complete! or I should say that I need to be able to respond with a success or error depending on what the result is. – chuckd Dec 20 '17 at 08:03
  • 1
    Not sure how this relates to my answer. Making action asynchronous does not change functional behavior. When `await` is called, the flow will not continue until awaited operation is completed. – CodeFuller Dec 20 '17 at 08:30
  • exception thrown with your example "System.InvalidOperationException: An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it." – chuckd Dec 20 '17 at 09:13
  • FYI - the async method is being called from a method in a service class method that the controller action method calls, not in the controller action method itself – chuckd Dec 20 '17 at 09:15
  • So have you marked your controller action `async` and returning `Task`? If you still have experience the problems, please provide the code of your action, little things could matter here. – CodeFuller Dec 20 '17 at 09:17