服务结构AspNet Core 3.1 Autofac WebHostBuilder [英] Service Fabric AspNet Core 3.1 Autofac WebHostBuilder
问题描述
我有服务结构无状态的asp.net core 2.2应用程序.我试图将其升级到asp.net core 3.1.我正在使用autofac依赖项注入容器.根据autofac文档,DI注册已移动从WebHostBuilder到通用HostBuilder https://github.com/microsoft/service-fabric-aspnetcore/issues/48 .
I have service fabric stateless asp.net core 2.2 application. I trying to upgrade this to asp.net core 3.1. I am using autofac dependency injection container. As per autofac documentation DI registration moved from WebHostBuilder to Generic HostBuilder https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting. But Service fabric doesn't support asp.net core Generic Host https://github.com/microsoft/service-fabric-aspnetcore/issues/48.
还有其他方法可以在WebHostBuilder中注册Autofac吗?
Is there any other way register Autofac in WebHostBuilder?
推荐答案
I think the official stance is that you should provide the generic host implementation yourself (https://github.com/Microsoft/service-fabric-aspnetcore/issues/48)
但是,我确实认为我为您提供了一种解决方法(我自己才开始使用此方法).您需要对其进行修改以配置您所做的任何事情,但重要的一行是 services.Replace(ServiceDescriptor.Singleton< IServiceProviderFactory< ContainerBuilder>>(new AutofacServiceProviderFactory(null)));
I do however think that I have a workaround for you (I just started using this myself). You need to modify it to configure whatever you do, but the important line is services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<ContainerBuilder>>(new AutofacServiceProviderFactory(null)));
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new[]
{
new ServiceInstanceListener(
serviceContext => new KestrelCommunicationListener(
serviceContext,
(url, listener) =>
{
return WebHost
.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<ContainerBuilder>>(new AutofacServiceProviderFactory(null)));
services.AddSingleton(serviceContext)
})
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.UseUniqueServiceUrl | ServiceFabricIntegrationOptions.UseReverseProxyIntegration)
.UseStartup<TStartupType>()
.Build();
}))
};
}
这篇关于服务结构AspNet Core 3.1 Autofac WebHostBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!