1
votes

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.

Is there any other way register Autofac in WebHostBuilder?

1
Welcome to SO! When you place a question try to add a minimum content: input sample, expected output sample, what did you try, research and where are you stuck. So leave a question, show us your code, what did you try and your research: stackoverflow.com/help/how-to-askborchvm
Did you find out anything to help you with this? I'm having the same issuediegosasw

1 Answers

1
votes

I think the official stance is that you should provide the generic host implementation yourself (https://github.com/Microsoft/service-fabric-aspnetcore/issues/48)

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();
                    }))
    };
}