Service Fabric 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://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting.但是 Service Fabric 不支持 asp.net core Generic Host 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?
推荐答案
我认为官方的立场是你应该自己提供通用主机实现(https://github.com/Microsoft/service-fabric-aspnetcore/issues/48)
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();
}))
};
}
这篇关于Service Fabric AspNet Core 3.1 Autofac WebHostBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!