带有现有IoC容器的ASP.NET Core环境? [英] ASP.NET Core with existing IoC container & environment?

查看:120
本文介绍了带有现有IoC容器的ASP.NET Core环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在已经托管现有应用程序的Windows服务环境中与 MVC 一起运行 ASP.NET Core Web堆栈,以便提供前端为了它.该应用程序使用Autofac来解决DI问题,这很好,因为它已经具有Microsoft.Extensions.DependencyInjection的扩展名(ASP.NET Core高度依赖该扩展名).

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

现在,我的问题是:ASP.NET Core希望在IoC容器中注册其自己的服务,此阶段已经构建了我自己的容器.我可以看到与ASP.NET Core一起运行自己的应用程序部分"的唯一方法是在ASP.NET Core Web主机Startup类的范围内设置应用程序,这似乎使ASP.NET Core成为现实.表现得像功能完善的应用程序框架,而不是网络框架.我还尝试了完全删除启动并像这样设置Web主机:

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

但是,这不起作用,因为在构建Web主机后,ASP.NET Core似乎会覆盖所有配置.因此,有没有办法在现有环境中集成 ASP.NET Core MVC ?也许是通过手动设置而不是使用WebHostBuilder等?

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

推荐答案

我发现的唯一方法是使用Autofac容器的Update()函数.但是Autofac lib将此方法标记为不良做法.

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

在Startup类之外:

Outside of Startup class:

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

在Startup类中:

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

请确保它是一个错误的hack +我建议避免这种方法.

但是答案

不要在Startup.Configure之前构建您的容器

don't build your container before the Startup.Configure

并非始终适用.就我而言,我使用了Azure Service Fabric .Net Core WebAPI无状态服务,并且在Startup内部构建容器的建议是错误的,因为我需要在Startup运行之前注入StatelessService.

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.

这篇关于带有现有IoC容器的ASP.NET Core环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆