[英]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 Core和MVC ,而不是相反? 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.