[英]ASP.NET Core - Registering for Application Shutdown isn't working
I am registering for application shutdown but my method is not called (logs are not printed) when I stop the application.我正在注册应用程序关闭,但是当我停止应用程序时没有调用我的方法(不打印日志)。
Using new class: Microsoft.Extensions.Hosting.IHostApplicationLifetime
使用新的 class:
Microsoft.Extensions.Hosting.IHostApplicationLifetime
I am running and stopping my app in Visual Studio Code.我正在 Visual Studio Code 中运行和停止我的应用程序。 My
Startup.cs
looks like this:我的
Startup.cs
看起来像这样:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
lifetime.ApplicationStopping.Register(OnShutdown);
lifetime.ApplicationStopped.Register(OnShutdown);
}
protected void OnShutdown()
{
Console.WriteLine("APPLICATION IS SHUTTING DOWN");
Debug.WriteLine(">>Stopped");
}
If you're stopping the application by killing the debugger (ie pressing the "stop" button), then it's not going to call those.如果您通过终止调试器(即按下“停止”按钮)来停止应用程序,那么它不会调用这些。 The program is dead the second you press it.
该程序在您按下它的那一刻就死了。
You would need to figure out a way to get SIGINT to the program.您需要想办法让 SIGINT 进入程序。 I don't know if that's possible or not in VS Code.
我不知道这在 VS Code 中是否可行。
If you use IIS express you can do there by clicking 'Stop Site' or 'Stop All'.如果您使用 IIS express,您可以通过单击“停止站点”或“停止所有”来完成。 This also works for IIS express.
这也适用于 IIS 快递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.