Service Fabric AspNet Core 3.1 Autofac WebHostBuilder

问题描述 投票:0回答:2

我有服务结构无状态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吗?

autofac azure-service-fabric asp.net-core-3.1 service-fabric-stateless
2个回答
2
投票

我认为官方立场是您应该自己提供通用主机实现(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();
                    }))
    };
}

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.