Reputation: 519
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.
Upvotes: 6
Views: 1500
Reputation: 393
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
Upvotes: 0