检查启动条件 [英] Check conditions on startup

查看:81
本文介绍了检查启动条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ASP.Net Core 2.0应用程序启动时测试某些条件.例如,如果我的数据库服务器或其他服务器正常运行.这对于仅在请求后才能实例化的事物(例如我的存储库)特别有用.

I would like to test certain conditions on Startup of my ASP.Net Core 2.0 application. For example if my database server or other is running correctly. This is especially helpful for things that will only be instantiated after a request (like my repository).

当前,我必须手动执行此请求,但是我希望我的应用程序尽早失败.建议在何时何地进行这样的测试?

Currently I have to do this request manually, but I would like to have my application fail early. At what moment and in what place is such a test recommended?

推荐答案

Startup类负责设置服务器,使其成为为应用程序设置一次性初始化工作的理想之选.

The Startup class is responsible for setting up your server, making it the perfect candidate for setting up one-time initialization stuff for your application.

Startup中通常有两个主要方法:ConfigureServicesConfigure.前者运行得很早,并且负责设置应用程序服务,依赖项和配置.因此,您不能使用它来实际执行实际工作,尤其是因为依赖项注入容器尚未准备好.

You usually have two main methods in Startup: ConfigureServices and Configure. The former runs very early and is responsible for setting up the application services, dependencies and configuration. So you cannot use it to actually perform real work, especially since the dependency injection container is not ready yet.

但是,Configure方法是不同的:尽管其主要目的是建立应用程序中间件管道,这些稍后将为请求提供服务的组件,您仍可以在此处充分使用依赖项,从而有可能做到这一点.这里有更广泛的内容.因此,您可以直接在这里拨打电话.

However, the Configure method is different: While its main purpose is to set up the application middleware pipeline, the components that will later serve requests, you are able to fully use your dependencies here, making it possible to already do more extensive things here. So you could make your calls directly here.

重要的是要了解Configure仍然运行得相当早,这实际上是在服务器实际上准备好处理请求之前.因此,如果您的初始化依赖于已经存在的实际服务器,则可能应该进一步延迟执行.

It’s important to understand that Configure still runs rather early, way before your server is actually ready to serve requests. So if your initialization depends on the actual server being around already, you should probably further delay the execution.

使用 ApplicationStarted 事件在服务器刚刚完成其设置阶段并准备好处理请求时运行.因此,基本上可以说是运行一些额外初始化的理想的空闲时刻.

The proper solution is likely to hook into the application lifecycle using IApplicationLifetime. This type basically offers you a way to register callbacks that are executed during the application lifecycle. In your case, you would be interested in the ApplicationStarted event which runs when the server just completed its setup phase and is now ready to serve requests. So basically the perfect idle moment to run some additional initialization.

为了响应生命周期事件,您需要在Configure方法内注册处理程序:

In order to respond to a lifetime event, you need to register your handler inside the Configure method:

public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime)
{
    // other…

    // register lifetime event
    applicationLifetime.ApplicationStarted.Register(InitializeApplication);
}

public void InitializeApplication()
{
    // do stuff
}

最后一点:显然,目前有一个会阻止生命周期的开放错误在IIS上托管时触发事件.在这种情况下,最好直接在Configure中执行代码.

One final note: Apparently, there is currently an open bug that prevents lifetime events from firing when hosting on IIS. In that case, executing your code directly in Configure is probably the best alternative.

这篇关于检查启动条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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