rabbitmq.net-coremasstransitrequest-response

RequestTimeoutException in Request/Response scenario


Trying to implement a request/response scenario with MassTransit.RabbitMQ version 4.0.1.1378-develop, a ASP.NET Core 2 Web application and a .NET Core 2 Console Project.

Below is the code that creates bus and request client in my ASP.NET Core 2.0 application:

var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
    sbc.Host(new Uri("rabbitmq://localhost"), h =>
    {
        h.Username("guest");
        h.Password("guest");
    })
);
services.AddSingleton<IPublishEndpoint>(bus);
services.AddSingleton<IBusControl>(bus);
var requestTimeout = TimeSpan.FromSeconds(10);
var address = new Uri("rabbitmq://localhost/myqueue");
services.AddScoped<IRequestClient<SubmitOrder, OrderSubmitResult>>(ctx => new MessageRequestClient<SubmitOrder, OrderSubmitResult>(bus, address, requestTimeout));

Then using the IRequestClient<SubmitOrder, OrderSubmitResult> that's been injected into my controller, I send the request as below:

private readonly IRequestClient<SubmitOrder, OrderSubmitResult> _client;
public async Task<ActionResult> SendRequest(string id)
{
    var result = await _client.Request(new SubmitOrder());
    ...
}

Finally, the ways I have tried to send response to the client:

var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
    var host = cfg.Host(new Uri("rabbitmq://localhost"), h =>
    {
        h.Username("guest");
        h.Password("guest");
    });
    cfg.ReceiveEndpoint(host, "myqueue", ep =>
    {
        ep.Handler<SubmitOrder>(context => context.RespondAsync(new OrderSubmitResult()));

        // async version
        //ep.Handler<SubmitOrder>(async context =>
        //{
        //  await context.RespondAsync(new OrderSubmitResult());
        //});

        // with a Consumer
        //ep.Consumer<SubmitOrderConsumer>();

        // with a single Consumer
        //ep.Instance(new SubmitOrderConsumer());
    });
});
bus.Start();

I get the request in my Bus, and send appropriate response. But I always get RequestTimeoutException in the client side and none of the above approaches worked.

Update:

Code is OK. I forgot to start the bus (bus.start) on client side :/

@Chris: Thanks for adding a full working sample in a short time. I suggest adding services.AddSingleton<IBusControl>(bus); and then change Startup.cs's Configure method to below:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime applicationLifetime, IBusControl bus)
{
    applicationLifetime.ApplicationStopping.Register(() => bus.Stop(TimeSpan.FromSeconds(10)));
....

To handle a graceful shutdown.


Solution

  • I've built a sample to show how to do this properly, including the use of ASP.NET Core 2, both a Web Application and a Console Application.

    https://github.com/MassTransit/Sample-DotNetCore-Request

    Enjoy!