net core 2.2 to 3.1 recently and having an issue with Autofact. I will need to do some initialisation after container is built.
In Asp.net Core 2.2, I have the following code in Startup.cs
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// do some other registr
builder.Populate(services);
IContainer container = builder.Build();
// Do some initialisation
container.Resolve<IMessageQueueService>().RegisterBodiesListener();
return new AutofacServiceProvider(container);
}
In the above code, IMessageQueueService
is inherited from another interface IService
and injected to container with
builder.RegisterAssemblyTypes(GetType().Assembly)
.As(typeof(IService))
.AsImplementedInterfaces();
However, in Asp.net Core 3.1 since the Build()
will be handled by Autofac itself, where I can do the initialisation?
Thank you
I got it. There is a callback we can use when Autofac builds
builder.RegisterBuildCallback(c => {
c.Resolve<IMessageQueueService>();
});
Refer to This Autofac Document