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.

winromulus
  • 469
  • 4
  • 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: https://github.com/MassTransit/MassTransit/blob/develop/src/Containers/MassTransit.Containers.Tests/Autofac_Tests/Autofac_Consumer.cs#L32 – Chris Patterson Aug 25 '19 at 20:21
  • @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 https://masstransit-project.com/MassTransit/usage/containers/autofac.html#using-a-module 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. – winromulus Aug 26 '19 at 08:35
  • The registrar for autofac just uses `builder.RegisterType()`, 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. – Chris Patterson Aug 26 '19 at 12:29
  • 1
    @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. – winromulus Aug 28 '19 at 12:53
  • 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? – ewomack Feb 28 '20 at 14:06

1 Answers1

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

karolgro
  • 181
  • 1
  • 8