我有服务结构无状态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 通用主机 https://github.com/microsoft/service-fabric-aspnetcore/issues/48。
还有其他方法在WebHostBuilder中注册Autofac吗?
我认为官方立场是您应该自己提供通用主机实现(https://github.com/Microsoft/service-fabric-aspnetcore/issues/48)
不过,我确实认为我有一个适合您的解决方法(我自己刚刚开始使用这个)。你需要修改它来配置你所做的任何事情,但重要的一行是
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();
}))
};
}
现在从 Microsoft.ServiceFabric.AspNetCore.Kestrel v5.2.1363 开始支持此功能:https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication-aspnetcore #ihost 和最小托管集成