question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with using Wait() method in ASP.NET MVC.

See original GitHub issue

The 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:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
sq735commented, Feb 13, 2017

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

1reaction
mycroescommented, Feb 14, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found