gpt4 book ai didi

c# - 来自测试的 Autofac RegisterAssemblyTypes

转载 作者:行者123 更新时间:2023-12-04 10:53:46 25 4
gpt4 key购买 nike

在我的 Startup I 类有 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<>));
}
}

使用 Autofac 注册程序集的建议方法是什么? (例如, RegisterAssemblyTypes )当 Startup从集成测试中调用类;例如。,

public class IntegrationTestApplicationFactory : WebApplicationFactory<Startup>
{ }

我正在使用 Autofac 4.9.4。

题:

您如何在 WebApplicationFactory<Startup> 中注册程序集类型使用 Autofac ?

最佳答案

由于不完整的文档和来自 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>();
}
}
}

关于c# - 来自测试的 Autofac RegisterAssemblyTypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59330674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com