Masstransit: Consumer twice

Created on 25 Jul 2019  Â·  11Comments  Â·  Source: MassTransit/MassTransit

I just follow the example :

builder.AddMassTransit(x =>
             {
                 //x.AddConsumers(Assembly.GetExecutingAssembly());
                 x.AddConsumer<DoSomethingConsumer>();
                 x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
                 {
                     var host = cfg.Host(new Uri("rabbitmq://localhost/SkyWatch"), h =>
                     {
                         h.Username("SkyWatch");
                         h.Password("sky_watch_2019_best");

                     });

                     cfg.ReceiveEndpoint("web-service-endpoint", ec =>
                     {

                         //ec.ConfigureConsumers(context);
                         ec.Consumer<DoSomethingConsumer>();
                     });

                     // or, configure the endpoints by convention
                     cfg.ConfigureEndpoints(context);

                 }));

                 //x.AddRequestClient<DoSomething>(new Uri("rabbitmq://localhost/SkyWatch/web-service-endpoint"));
             });

call consumer twice
but when I remove " x.AddConsumer();" it works fine. how

Most helpful comment

Yeah, you're using every example instead of just one approach.

Use either:

builder.AddMassTransit(x =>
{
    x.AddConsumers(Assembly.GetExecutingAssembly());

    x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
    {
        var host = cfg.Host(new Uri("rabbitmq://localhost/Test1"), h =>
        {
            h.Username("SkyWatch");
            h.Password("sky_watch_2019_best");
        });

        cfg.ConfigureEndpoints(context);
    }));
});

or

builder.AddMassTransit(x =>
{
    x.AddConsumers(Assembly.GetExecutingAssembly());

    x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
    {
        var host = cfg.Host(new Uri("rabbitmq://localhost/Test1"), h =>
        {
            h.Username("SkyWatch");
            h.Password("sky_watch_2019_best");
        });

        cfg.ReceiveEndpoint("web-service-endpoint", ec =>
        {
            ec.ConfigureConsumers(context);
        });    
    }));
}); 

All 11 comments

I think its because you registering same consumer twice on an endpoint. That extension that you are using ec.Consumer<DoSomethingConsumer>(); is not container integration extension method so it will use standard functionality for resolving consumer for a message. If you want to register and use single consumer use container extension method something like ec.Consumer<DoSomethingConsumer>(IContainer); If you want to scan assembly and add consumers by convention use 'x.AddConsumers(assembly);' and then ec.ConfigureConsumers(context);

I already follow you said, "x.AddConsumers(assembly) and ec.ConfigureConsumers(context)"
and clean my rabbitmq, but still call consumer twice

builder.AddMassTransit(x =>
             {
                 x.AddConsumers(Assembly.GetExecutingAssembly());
                 //x.AddConsumer<DoSomethingConsumer>();
                 x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
                 {
                     var host = cfg.Host(new Uri("rabbitmq://localhost/Test1"), h =>
                     {
                         h.Username("SkyWatch");
                         h.Password("sky_watch_2019_best");

                     });

                     cfg.ReceiveEndpoint("web-service-endpoint", ec =>
                     {

                         ec.ConfigureConsumers(context);
                         //ec.Consumer(typeof(DoSomethingConsumer),  c => Activator.CreateInstance(c));
                         //ec.Consumer<DoSomethingConsumer>();
                     });

                     // or, configure the endpoints by convention
                     cfg.ConfigureEndpoints(context);

                 }));

                 //x.AddRequestClient<DoSomething>(new Uri("rabbitmq://localhost/SkyWatch/web-service-endpoint"));
             });

ec.ConfigureConsumers(context); - configures registered consumers on given endpoint
cfg.ConfigureEndpoints(context); - configures registered consumers on all endpoints
If you only have one endpoint remove either of these and it should work.

Yeah, you're using every example instead of just one approach.

Use either:

builder.AddMassTransit(x =>
{
    x.AddConsumers(Assembly.GetExecutingAssembly());

    x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
    {
        var host = cfg.Host(new Uri("rabbitmq://localhost/Test1"), h =>
        {
            h.Username("SkyWatch");
            h.Password("sky_watch_2019_best");
        });

        cfg.ConfigureEndpoints(context);
    }));
});

or

builder.AddMassTransit(x =>
{
    x.AddConsumers(Assembly.GetExecutingAssembly());

    x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
    {
        var host = cfg.Host(new Uri("rabbitmq://localhost/Test1"), h =>
        {
            h.Username("SkyWatch");
            h.Password("sky_watch_2019_best");
        });

        cfg.ReceiveEndpoint("web-service-endpoint", ec =>
        {
            ec.ConfigureConsumers(context);
        });    
    }));
}); 

OK, thanks all. I'll give it a try.

Yeah, you're using every example instead of just one approach.

Use either:

builder.AddMassTransit(x =>
{
    x.AddConsumers(Assembly.GetExecutingAssembly());

    x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
    {
        var host = cfg.Host(new Uri("rabbitmq://localhost/Test1"), h =>
        {
            h.Username("SkyWatch");
            h.Password("sky_watch_2019_best");
        });

        cfg.ConfigureEndpoints(context);
    }));
});

or

builder.AddMassTransit(x =>
{
    x.AddConsumers(Assembly.GetExecutingAssembly());

    x.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
    {
        var host = cfg.Host(new Uri("rabbitmq://localhost/Test1"), h =>
        {
            h.Username("SkyWatch");
            h.Password("sky_watch_2019_best");
        });

        cfg.ReceiveEndpoint("web-service-endpoint", ec =>
        {
            ec.ConfigureConsumers(context);
        });    
    }));
}); 

It works! It should be in the examples. Thanks!

Hello.
Can I configure consumer for special endpoints and others automatically configure by default (by ConfigureEndpoints)?

I'm not sure I entirely understand your question, but you can also manually configure ReceiveEndpoint()'s and use ConfigureEndpoints(). All of the receive endpoints will be configured.

I registered 10 consumers, but for one of them I want set a special endpoint and for others 9 make a default endpoint. If I will use ReceiveEndpoint() and ConfigureEndpoints() together, consumer with special endpoint will work twice: message from special endpoint and from default.

You can create a consumer definition, or you can configure the endpoint when registering the consumer.

https://masstransit-project.com/usage/containers/definitions.html

Or Use

x.AddConsumer<T>()
    .Endpoint(e => ...);

You can create a consumer definition, or you can configure the endpoint when registering the consumer.

https://masstransit-project.com/usage/containers/definitions.html

Or Use

x.AddConsumer<T>()
    .Endpoint(e => ...);

Thank you very much, this is that I wanted!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ashtonian picture Ashtonian  Â·  6Comments

ghd258 picture ghd258  Â·  6Comments

ffMathy picture ffMathy  Â·  3Comments

eggertcosta picture eggertcosta  Â·  4Comments

danielman111 picture danielman111  Â·  4Comments