我对创建和注册模块很陌生,但我试图更好地理解整个过程。目前我正在尝试将依赖项注入到 autofac 模块中,但我认为问题在于 autofac 在依赖项注入发生之前注册了它的模块。这是我的安全模块:
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();
}
}
这是我在 program.cs 中注册模块的部分:
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);
});
}
这是我收到的错误。有没有办法指定这个模块应该在以后注册?还是我错过了重要的信息?
DependencyResolutionException:无法使用可用的服务和参数调用类型“inQuba.CX.Web.Api.AutofacIntegration.SecurityModule”上的“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”的构造函数:无法解析参数“Microsoft。构造函数“Void .ctor(Microsoft.Extensions.Configuration.IConfiguration)”的 Extensions.Configuration.IConfiguration 配置。