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
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.