How to use Service Fabric service with AspNet Core WebApi and Autofac and run TestServer

562 views Asked by At

I can't figure out how to use an AspNet Core 3.1 Web Api with Service Fabric and Autofac, and also how to have it ready for a TestServer to run for integration/functional testing.

The documentation is very incomplete.

Autofac documentation shows how to modify Program.cs to build autofac container, but it does not mention anything about the Startup.cs class that all the web api have: https://autofaccn.readthedocs.io/en/latest/integration/servicefabric.html

Also the only example that Autofac has for service fabric is not a web api: https://github.com/autofac/Examples/tree/master/src/ServiceFabricDemo

There are other questions without valid answers: Service Fabric AspNet Core 3.1 Autofac WebHostBuilder

Does anybody have any example on how to achieve this?

I can achieve the following (please see my GitHub repository with the sample)

  • Service fabric with stateless AspNet Core WebApi project (dotnet core 3.1)
  • Using Microsoft Dependency Injection to register services
  • Using TestServer to run integration tests on the http endpoint, and able to overwrite dependency injection registrations in a clean way without having to create another Startup class

I want the exact same, but using Autofac as DI container.


UPDATE 1: I can't add Autofac to a WebHostBuilder and the ConfigureServices(IServiceCollection services) must be void as per AspNet Core 3.1+, so this is where I'm stuck. How to replace MS Dependency Injection in my sample

2

There are 2 answers

0
diegosasw On BEST ANSWER

Event after the bounty there is not an answer to this. Maybe it's not possible as service fabric requires WebHost and not generic host.

In any case, I managed to have it working with older versions. Here's my repository where I show a sample on how to run AspNetCore2.1 with DotNetCore2.1 (LTS) and Autofac under Service Fabric. I use the webhost builder, not the generic one.

https://gitlab.com/sunnyatticsoftware/training/sasw-aspnetcore-testing/-/tree/master/aspnetcore2_1_autofac_servicefabric

Still, it'd be nice to eventually have a valid answer to the question.

0
Esben Bach On

I have NO idea if this works for the TestServer. It does however work fine with during ordinary hosting.

The exact thing you are looking for would be this line: 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();
                    }))
    };
}

Hope it helps, it took me quite a while to arrive at this solution.