Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async/await broken in ASP.NET projects #4

Closed
b-szypelow opened this issue Nov 7, 2017 · 3 comments
Closed

async/await broken in ASP.NET projects #4

b-szypelow opened this issue Nov 7, 2017 · 3 comments

Comments

@b-szypelow
Copy link

I'm using Mediator.Net with Autofac in an ASP.NET WebApi2 project. I've found out the IRequestHandler is not working properly when it's an async method with awaits.

I was able to reproduce the issue on a blank new WebApi project with the following code:

    public class DefaultController : ApiController
    {
        private readonly IMediator m;
        public DefaultController(IMediator m) => this.m = m;

        [Route("ping"), HttpGet]
        public async Task<IHttpActionResult> Ping() 
            => Ok(await m.RequestAsync<Ping, Pong>(new Ping()));

        [Route("hang"), HttpGet]
        public async Task<IHttpActionResult> Ping2()
            => Ok(await m.RequestAsync<Ping2, Pong2>(new Ping2()));
    }

    public class Ping : IRequest { }
    public class Ping2 : IRequest { }
    public class Pong : IResponse { }
    public class Pong2 : IResponse { }

    public class WorkingHandler : IRequestHandler<Ping, Pong>
    {
        public Task<Pong> Handle(ReceiveContext<Ping> context)
        {
            return Task.FromResult(new Pong());
        }
    }

    public class BrokenHandler : IRequestHandler<Ping2, Pong2>
    {
        public async Task<Pong2> Handle(ReceiveContext<Ping2> context)
        {
            await Task.Yield(); // Or whatever awaitable
            return new Pong2(); // Never reached
        }
    }

The Configuration is as folllows:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            var b = new ContainerBuilder();
            b.RegisterApiControllers(Assembly.GetExecutingAssembly());
            b.RegisterMediator(new MediatorBuilder().RegisterHandlers(Assembly.GetExecutingAssembly()));
            var c = b.Build();

            config.DependencyResolver = new AutofacWebApiDependencyResolver(c);
        }
    }

The /ping endpoint works as expected. The /hang endpoint never terminates.

I was not able to reproduce the issue in unit tests. It only happens in ASP.NET.

My first guess is that it's a deadlock caused by a blocking call of Result in RequestPipe.cs which interfered with the way IIS is handling tasks.

@mayuanyang
Copy link
Owner

Hi @b-szypelow,

Thanks for keeping in touch, will dig in a bit more and review your PR soon.

Regards
Eddy

@mayuanyang
Copy link
Owner

Have merged the PR and push a new version to nuget, please update and validate. thanks

@b-szypelow
Copy link
Author

I've updated my project and it works now. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants