How to configure MassTransit with Autofac Module?
Asked Answered
L

1

6

I'm trying to setup MassTransit with Autofac (latest NuGet packages) but it seems that consumers cannot be registered at the ContainerBuilder level. Has anyone managed to use MassTransit with Autofac, especially with modules?

In the sample below, the DoSomethingConsumer cannot be resolved, unless I uncomment the x.AddConsumer line and comment the registration on the builder.

           builder.RegisterType<BusControlHostedService>().As<IHostedService>();
            builder.RegisterType<DoSomethingConsumer>().AsSelf().AsImplementedInterfaces();

            builder.AddMassTransit(x =>
            {
                //x.AddConsumer<DoSomethingConsumer>();

                x.AddBus(componentContext => Bus.Factory.CreateUsingRabbitMq(cfg =>
                {
                    cfg.Host("host", "vhost", h =>
                    {
                        h.Username("user");
                        h.Password("password");
                    });

                    cfg.UseExtensionsLogging(componentContext.Resolve<ILoggerFactory>());

                    cfg.ConfigureEndpoints(componentContext);

                }));

                x.AddRequestClient<DoSomethingRequest>();
            });

The documentation at https://masstransit-project.com/MassTransit/usage/containers/autofac.html seems to be wrong. There is no .AddConsumer extension method on the builder.

I'm looking to register consumers from modules and be able to configure endpoints by convention (as in the sample above) or using .RegisterEndpoint.

Any help on this would be greatly appreciated.

Lotti answered 25/8, 2019 at 17:52 Comment(5)
Are you using a recent version of MassTransit? The documentation is correct, and you can see the unit tests for the container as well: github.com/MassTransit/MassTransit/blob/develop/src/Containers/…Malonylurea
@ChrisPatterson, as mentioned, I'm using the lastest version of MassTransit. In the link you posted the consumer is not added to the ContainerBuilder (to builder ) but inside the configuration action for MassTransit, which is not what I need. If you look at the masstransit-project.com/MassTransit/usage/containers/… documentation, it shows how to do this via modules, which is what I need, but that documentation is either wrong or out of date since the methods used do not exist. Also seems there is some Ninject or Structuremap config in there too.Lotti
The registrar for autofac just uses builder.RegisterType<T>(), then you could ask the container for all types which implement IConsumer and add them to the registration in the AddMassTransit section, similar to how they're added by namespace.Malonylurea
@ChrisPatterson I agree that there are workarounds but I want to do this the clean Autofac way. It seems there is stuff built-in the API for it but I'm not sure how to get it set up.Lotti
I am on a project trying to use Autofac modeuls with MassTransit as well and the module we have setup, which follows the documentation posted above, does not register when injected into a service. Any updates on this one?Cantlon
S
0

The problem is caused by the fact that ASP.NET Core doesn't request controllers from DI container (IServiceProvider)- it asks for their ctor params instead. So Autofac doesn't see the controllers. You can use AddControllersAsServices(), but that will work only for controllers registered before this call. So you need to first register the controllers, then call AddControllersAsServices() and it should help

Sidonnie answered 21/9, 2022 at 17:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.