在我的Startup类中,我通过autofac为RegisterAssemblyTypes提供了一个have方法,如下所示:
public class Startup
{
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterModule(new ApplicationModule());
}
}
public class ApplicationModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder
.RegisterAssemblyTypes(
typeof(EventHandler).GetTypeInfo().Assembly)
.AsClosedTypesOf(typeof(IIntegrationEventHandler<>));
}
}在从集成测试中调用Autofac类时,使用RegisterAssemblyTypes注册程序集的建议方法是什么;例如,
public class IntegrationTestApplicationFactory : WebApplicationFactory<Startup>
{ }我正在使用Autofac 4.9.4。
问题:
如何在WebApplicationFactory<Startup>中使用Autofac注册程序集类型
发布于 2019-12-19 22:31:08
由于文档不完整,以及Autofac维护人员的支持很差,我们决定在我们研究所的所有项目中恢复Autofac的使用。
现在,我们使用ASP.NET Core的内建注册,当在以下方法中调用时,它可以按照预期的方式工作:
public class IntegrationTestApplicationFactory : WebApplicationFactory<Startup>
{
protected override IHost CreateHost(IHostBuilder builder)
{
// register services here,
// or in the ConfigureWebHost method
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
services.AddTransient<YourServiceType>();
}
}
}https://stackoverflow.com/questions/59330674
复制相似问题