在.NET Core中的ConfigureService-Methods(尤其是IApplicationLifetime)中使用DI [英] Using DI in ConfigureService-Methods (especially IApplicationLifetime) in .NET Core

查看:272
本文介绍了在.NET Core中的ConfigureService-Methods(尤其是IApplicationLifetime)中使用DI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 IWebHostBuilder 实现新的扩展方法,该方法执行一些代码,例如在某个地方注册。
重要的是,它在应用程序关闭时也会注销。

I'm trying to implement a new extension method for the IWebHostBuilder that executes some code, like registering somewhere. The important part is, that it also unregisters when the application is shutting down.

我添加了一些代码,这些代码显示了我想做的事情。这里的问题是 IApplicationLifetime 的实例尚不可用。在构建WebHost的管道中,最后添加了此新扩展方法。

I added some code which shows what I want to do. The problem here is, that the instance of IApplicationLifetime is not available yet. This new extension method was added last in the pipeline of building the WebHost.

public static IWebHostBuilder UseHttpGatewayappRegistration(this IWebHostBuilder webHostBuilder)
    {
        webHostBuilder.ConfigureServices(services =>
        {
            var sp = services.BuildServiceProvider();

            // This instance is not available
            IApplicationLifetime applicationLifetime = sp.GetService<IApplicationLifetime>();

            // These instances are ok
            AppSettings appSettings = sp.GetService<AppSettings>();
            ILoggerFactory loggerFactory = sp.GetService<ILoggerFactory>();

            var registration = new Registration(loggerFactory.CreateLogger<Registration>());

            applicationLifetime.ApplicationStopping.Register(() =>
            {
                registration.Unregister();
            });
        });

        return webHostBuilder;
    }

为什么 IApplicationLifetime 实例为null,即使我在构建WebHost管道的管道中最后添加了此扩展方法?如果有人能向我提供有关所有 ConfigureServices方法的执行顺序的信息,以及如何或完全可以在其中使用 IApplicationLifetime 的信息,那将是很好的 ConfigureServices 方法。

Why is the IApplicationLifetime instance null, even though I added this extension method last in the pipeline of building the WebHost pipeline? It would be great, if someone would provide me with some information about the execution order of all "ConfigureServices" methods and how or if it is at all possible to use IApplicationLifetime in a ConfigureServices method.

我知道我可以在没有 WebHostBuilder 的情况下完成所有操作,但是在我看来这样做很合乎逻辑我也认为必须有一种方法。

I know I could do this all without the WebHostBuilder, but it seems logical to me to do it there and I also think there has to be a way.

很遗憾,我在网上找不到很多信息...

Unfortunately I couldn't find much information online...

谢谢。

编辑:我找到了一种在 ConfigureServices 方法中使用DI的方法。我相应地编辑了问题。这对我有帮助:如何在ASP中的ConfigureServices中解析实例.NET Core

EDIT: I found a way to use DI in ConfigureServices methods. I edited the question accordingly. This helped me: How to Resolve Instance Inside ConfigureServices in ASP.NET Core

推荐答案

您无法访问 IApplicationLifetime ConfigureServices 方法中的$ c>实例。这是设计使然。在此处检查启动中可用的服务部分:
应用程序在ASP.NET Core中启动

You can't get access to IApplicationLifetime instance from ConfigureServices method. It's by design. Check the "Services Available in Startup" section here: Application Startup in ASP.NET Core.

IWebHostBuilder中解析 IApplicationLifetime .Configure 方法(如果稍后在 IWebHostBuilder Startup.Configure 方法。 >配置管道):

Resolve IApplicationLifetime in the IWebHostBuilder.Configure method (it replaces the Startup.Configure method if it is used later in IWebHostBuilder configuration pipeline):

public static IWebHostBuilder Foo(this IWebHostBuilder webHostBuilder)
{
    webHostBuilder.Configure(x =>
    {
        var service = x.ApplicationServices.GetService<IApplicationLifetime>();
    });

    return webHostBuilder;
}

也可以扩展原始的 Startup.Configure 方法而不是 replace (应添加很多反射逻辑以使该解决方案可靠):

It's also possible to extend the original Startup.Configure method instead of replace (a lot of Reflection logic should be added to make this solution reliable):

.Configure(app =>
{
    var env = app.ApplicationServices.GetService<IHostingEnvironment>();
    dynamic startup = Activator.CreateInstance(typeof(TStartup), env);

    //Carefully resolve all input parameters.
    //Make sure all required services are registered in DI.
    startup.Configure(app, env);

    var lifetime = app.ApplicationServices.GetService<IApplicationLifetime>();
})

这篇关于在.NET Core中的ConfigureService-Methods(尤其是IApplicationLifetime)中使用DI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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