3

I'd like to run the ASP.NET Core web stack along with MVC within a Windows service environment which already hosts an existing application in order to provide a frontend for it. The application uses Autofac for DI concerns which is nice because it already has an extension for Microsoft.Extensions.DependencyInjection (on which ASP.NET Core heavily relies upon).

Now, my problem is: ASP.NET Core wants to register its own services within the IoC container, at a stage where my own container has already been built. The only way I can see to run my own 'application part' along with ASP.NET Core is by setting up my application within the scope of the ASP.NET Core web host Startup class, what appears to make ASP.NET Core behaving like a full blown application framework rather than web framework. What I also tried is dropping the Startup completely and setting the web host up like this:

        var containerBuilder    = new ContainerBuilder();
        var container           = containerBuilder.Build();
        var webHost             = new WebHostBuilder()
            .ConfigureServices(services =>
            {
                services.AddMvc();
            })
            .Configure(app =>
            {
                app.ApplicationServices = new AutofacServiceProvider(container);
                app.UseStaticFiles();
                app.UseMvc();
            })
            .UseKestrel()
            .UseIISIntegration()
            .Build();

        webHost.Run();

However this doesn't work because ASP.NET Core seems to override all configurations as soon as the web host is getting built. So, is there a way to integrate ASP.NET Core as well as MVC in an existing environment rather than the other way around? Maybe by setting it up manually rather than using WebHostBuilder etc.?

artganify
  • 683
  • 1
  • 8
  • 23
  • Simply, don't build your container before the `Startup.Configure` method is being called. In the Configure method you get a `IServiceCollection` passed in. There you register all your services and ASP.NET Core dependencies using the `services.AddXxx(...)` methods. Then you pass this to autofac: `containerBuilder.Populate(services)` to register them within Autofac, then build the container, and return it in the `Configure` method as seen in the documentation here: http://docs.autofac.org/en/latest/integration/aspnetcore.html – Tseng Aug 12 '16 at 12:57
  • @Tseng you should write that as an answer instead of a comment. – Brad Aug 12 '16 at 14:45
  • 2
    @Tseng Yes, I am aware of how it's supposed to be done, but that simply ignores the fact that the ASP.NET Core web application is not the composition root of my app, I just want to start the web framework as an extension, working with an existing container which has been created previously. – artganify Aug 12 '16 at 14:45
  • You can also update an already built container, but check the comment on this answer http://stackoverflow.com/a/5009312/455493. It says, it can result in indeterministic behavior if services were already resolved by the existing container. – Tseng Aug 15 '16 at 07:11

1 Answers1

1

The only way I found is using Update() function of Autofac container. But Autofac lib marks this approach as bad practice.

Outside of Startup class:

class Program {
   public static IContainer Container { get; private set; }
   
   void Main(){
      var builder = new ContainerBuilder();
      ...
      Container = builder.Build();
   }
}

And in Startup class:

 public abstract class Startup
 {
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
       var builder = new ContainerBuilder();
       builder.Populate(services);
       // Update existing container
       builder.Update(Program.Container);
    }
 }

For sure its a bad hack + I suggest to avoid this approach.

But the answer

don't build your container before the Startup.Configure

is not always applicable. In my case I used Azure Service Fabric .Net Core WebAPI stateless service and suggestion to build container inside Startup is wrong since I need to inject StatelessService before Startup runs.

Community
  • 1
  • 1
Pavel
  • 616
  • 1
  • 7
  • 13