I am quite new to creating and registering modules but I am trying to get a better understanding of the process as a whole. Currently I am trying to inject a dependency into an autofac module but I think the problem is that autofac registers its modules before the dependency injection can take place. Here is my my security module:
public class SecurityModule : Module
{
private readonly IConfiguration configuration;
public SecurityModule(IConfiguration configuration)
{
this.configuration = configuration;
}
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<TeamManager>()
.WithParameter("tenantName", this.configuration["TenantName"])
.InstancePerDependency();
}
}
Here is the section in program.cs where I register the module:
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterAssemblyModules(typeof(Startup).Assembly);
builder.RegisterModule<LoggingModule>();
builder.RegisterModule(new ConfigurationModule(args));
builder.RegisterModule<CXDatabaseModule>();
builder.RegisterModule<SecurityModule>();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddLog4Net();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(options =>
{
options.Limits.MaxRequestBodySize = 209715200;
});
webBuilder.UseStartup<Startup>();
})
.UseNServiceBus((configuration, container) =>
{
return CreateStartableEndpoint(configuration, container);
});
}
And this is the error I am receiving. Is there possibly a way to specify that this module should be registered at a later stage ? Or am I missing a vital bit of information?
DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'inQuba.CX.Web.Api.AutofacIntegration.SecurityModule' can be invoked with the available services and parameters: Cannot resolve parameter 'Microsoft.Extensions.Configuration.IConfiguration configuration' of constructor 'Void .ctor(Microsoft.Extensions.Configuration.IConfiguration)'.