Search code examples
.netrabbitmqmasstransitlinqpad

RequestTimeoutException when using MassTransit 8 requests in LINQPad 7


Still new to MassTransit and encountering problems when trying to use MT8 requests in LINQPad scripts. I have a RabbitMQ management instance spun up locally in Docker, which works with .NET console apps, but I must be missing some requirements for using MT8 directly from LINQPad 7.

Here's a basic example I made to demonstrate my error:

async Task Main()
{
    IServiceCollection services = new ServiceCollection();
    services.AddMassTransit(x =>
    {
        x.AddConsumer(typeof(MyConsumer));

        x.UsingRabbitMq((ctx, cfg) =>
        {
            cfg.Host("localhost", 5672, "/", h =>
            {
                h.Username("guest");
                h.Password("guest");
            });

            cfg.ConfigureEndpoints(ctx);
        });
    }); 
    var serviceProvider = services.BuildServiceProvider();
    
    var bus = serviceProvider.GetService<IBus>();
    var response = await bus.Request<MyRequest, MyResponse>(new MyRequest("Hi there"));
    response.Message.Dump("Response");
}

namespace MyNamespace
{
    public class MyRequest
    {
        public MyRequest() { }

        public MyRequest(string phrase)
        {
            Phrase = phrase;
        }

        public string Phrase { get; set; }
    }

    public class MyResponse
    {
        public MyResponse() { }

        public MyResponse(string phrase)
        {
            Phrase = phrase;
        }

        public string Phrase { get; set; }
    }

    public class MyConsumer : IConsumer<MyRequest>
    {
        public async Task Consume(ConsumeContext<MyRequest> context)
        {
            await context.RespondAsync(new MyResponse($"'{context.Message.Phrase}' to you too!"));
        }
    }
}

My RabbitMQ container seems to receive the message (and create a temporary exchange), but not be able to route it back...

enter image description here

Then after 30 seconds, the exception is thrown:

RequestTimeoutException: Timeout waiting for response

enter image description here

Messages send correctly from LINQPad with this method, and consumers set up in .NET console apps can consume them fine. But I can't make request/responses from LINQPad, which is my ultimate goal here.

Thanks in advance :)


Solution

  • Wow, it's been years since I've seen anyone use LINQpad.

    And even longer since I've had to tell people they need to start the bus.

    MassTransit uses a hosted service to start the bus, which is normally invoked by the underlying generic host built into .NET.

    Since you aren't using the host, you'll need get IBusControl from the container and start the bus. And yes, you'll need to stop it as well after you're done.