Problem with using Wait() method in ASP.NET MVC.
See original GitHub issueThe issue is clearly illustrated by this code:
public class TestCommand: IRequest
{ }
public class TestHandler : IAsyncRequestHandler<TestCommand>
{
public Task Handle(TestCommand message)
{
return Task.Factory.StartNew(() =>
{
throw new ApplicationException();
});
}
}
public class HomeController : Controller
{
private readonly IMediator _mediator;
public HomeController(IMediator mediator)
{
_mediator = mediator;
}
public ActionResult Index()
{
try
{
_mediator.Send(new TestCommand()).Wait(); // deadlock
}
catch (Exception e)
{
// unreachable block
}
return View();
}
}
If i use await
operator catch
block is reachable:
try
{
await _mediator.Send(new TestCommand());
}
catch (Exception e)
{
// OK
}
but when use Wait()
method i have a problem, because catch
block is unreachable.
try
{
_mediator.Send(new TestCommand()).Wait(); // deadlock
}
catch (Exception e)
{
// unreachable block
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
asp.net mvc - How to wait for first async await method ...
Run(async () => await SaveCustomerDataAsync(DocumentID, DocumentType, bytes, fileName)); In addition to being smaller code, the Task has much ...
Read more >Using Asynchronous Methods in ASP.NET MVC 4
In general, you should make a method asynchronous if the synchronous method waits on the ASP.NET request thread while doing no work. By...
Read more >Two Ways to Do Async/Await in ASP.NET Wrong (and How ...
Run() . Don't use asynchronous wrappers over synchronous calls. There are lots of ways to screw up your ASP.NET code that uses async/await....
Read more >How to justify using await instead of .Result() or .Wait() in . ...
This means that using .Result() or .Wait() after async method doesn't cause deadlock any more, and only affects performance (reserving a thread) ...
Read more >Understanding Control Flow with Async and Await in C# | ...
NET's HttpClient class is returning a Task instance, but we're calling GetAwaiter().GetResult() on the task, which is a blocking call. Again, this is ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Perhaps I found a lines of code which can cause problems, for example the deadlock. I think we needs to use
ConfigureAwait(false)
.RequestHandler.cs
RequestHandler.cs
RequestPostProcessorBehavior.cs
RequestPostProcessorBehavior.cs
RequestPreProcessorBehavior.cs
RequestPreProcessorBehavior.cs
P.S. This can be useful Don’t Block on Async Code
I agree about the
ConfigureAwait(false)
. MediatR doesn’t depend on the context, so there’s no need to have continuations run on the calling context. If a higher level does depend on context, that’ll still work as-is.