简体   繁体   中英

ASP.NET Core with existing IoC container & environment?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM