测试中的 Autofac RegisterAssemblyTypes
Autofac RegisterAssemblyTypes from tests
在我的 Startup
class 中,我有一个 RegisterAssemblyTypes
通过 autofac 的方法如下:
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<>));
}
}
当从集成测试中调用 Startup
class 时,使用 Autofac
(例如 RegisterAssemblyTypes
)注册程序集的建议方法是什么?例如,
public class IntegrationTestApplicationFactory : WebApplicationFactory<Startup>
{ }
我正在使用 Autofac 4.9.4。
问题:
如何使用 Autofac
在 WebApplicationFactory<Startup>
中注册程序集类型?
由于文档不完整以及 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>();
}
}
}
在我的 Startup
class 中,我有一个 RegisterAssemblyTypes
通过 autofac 的方法如下:
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<>));
}
}
当从集成测试中调用 Startup
class 时,使用 Autofac
(例如 RegisterAssemblyTypes
)注册程序集的建议方法是什么?例如,
public class IntegrationTestApplicationFactory : WebApplicationFactory<Startup>
{ }
我正在使用 Autofac 4.9.4。
问题:
如何使用 Autofac
在 WebApplicationFactory<Startup>
中注册程序集类型?
由于文档不完整以及 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>();
}
}
}