简体   繁体   English

ASP.NET Core与现有的IoC容器和环境?

[英]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. 我想在已经托管现有应用程序的Windows服务环境中运行ASP.NET Core Web堆栈和MVC ,以便为它提供前端。 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). 该应用程序使用Autofac进行DI关注,这很好,因为它已经有一个Microsoft.Extensions.DependencyInjection的扩展(ASP.NET Core很大程度上依赖它)。

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. 现在,我的问题是:ASP.NET Core希望在IoC容器中注册自己的服务,在我自己的容器已经构建的阶段。 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. 我可以看到与ASP.NET Core一起运行我自己的“应用程序部分”的唯一方法是在ASP.NET Core Web主机Startup类的范围内设置我的应用程序,这似乎使ASP.NET Core表现得像一个完整的应用程序框架而不是Web框架。 What I also tried is dropping the Startup completely and setting the web host up like this: 我还试过完全放弃Startup并像这样设置web主机:

        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. 但是这不起作用,因为一旦构建Web主机,ASP.NET Core似乎会覆盖所有配置。 So, is there a way to integrate ASP.NET Core as well as MVC in an existing environment rather than the other way around? 那么,有没有办法在现有环境中集成ASP.NET CoreMVC ,而不是相反? Maybe by setting it up manually rather than using WebHostBuilder etc.? 也许通过手动设置而不是使用WebHostBuilder等?

The only way I found is using Update() function of Autofac container. 我找到的唯一方法是使用Autofac容器的Update()函数。 But Autofac lib marks this approach as bad practice . 但Autofac lib将此方法视为不良做法

Outside of Startup class: 在Startup类之外:

class Program {
   public static IContainer Container { get; private set; }

   void Main(){
      var builder = new ContainerBuilder();
      ...
      Container = builder.Build();
   }
}

And in Startup class: 在Startup类中:

 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 不要在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. 在我的情况下,我使用Azure Service Fabric .Net Core WebAPI无状态服务和建议在Startup中构建容器是错误的,因为我需要在Startup运行之前注入StatelessService。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 3.x - 访问 IoC 容器 - ASP.NET Core 3.x - Access IoC Container 将所有注册的服务从 ASP.NET Core IOC 容器解析到单个接口 - Resolve all registered services to a single interface from ASP.NET Core IOC container Autofac 作为 AWS 中的 IoC 容器 Lambda 无服务器 ASP.NET Core 3.1 Web ZDB974238714CA8DE64FZACE7 - Autofac as IoC container in AWS Lambda Serverless ASP.NET Core 3.1 Web API 使用 ASP.NET Core 内置 IoC 容器注入多个客户端而不是一个 - Injecting multiple clients instead of just one using ASP.NET Core built-in IoC container 使用ASP.NET Core注册现有的Autofac容器 - Registering an existing Autofac container with ASP.NET Core 如何在ASP.NET中使用IoC容器配置单元测试? - How to configure unit tests with an IoC container in ASP.NET? 在ASP.net解决方案中使用Unity IOC容器 - Using Unity IOC Container in ASP.net solution 标准化存储库,UnitOfWork,IOC容器Asp.Net MVC - Standardize Repository, UnitOfWork, IOC Container Asp.Net MVC 具有IoC容器并发症的C#ASP.NET依赖注入 - C# ASP.NET Dependency Injection with IoC Container Complications 如何在ASP.net Core 2中使用Windsor IoC - How to use Windsor IoC in ASP.net Core 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM