简体   繁体   English

ASP.NET 内核 - 注册应用程序关闭不起作用

[英]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.

相关问题 ASP.NET 核心 MVC RedirectToAction 不工作 - ASP.NET Core MVC RedirectToAction isn't working Asp.Net Core 5 使用应用程序部件注册控制器 - Asp.Net Core 5 Registering Controllers using Application Parts .net核心应用程序中的CORS无法正常工作 - CORS in .net Core application isn't working 通过 asp-for="..." 将字符串值传递到 ASP.Net 核心中的 Model 无法正常工作 - Passing a String value through asp-for=“…” into a Model in ASP.Net core isn't working the way it should ASP.NET Core EventLog未注册 - ASP.NET Core EventLog Not Registering 调试 IIS 网站时,ASP.NET Core 2 Web 应用程序未加载用户机密 - ASP.NET Core 2 web application isn't loading user secrets when debugging IIS website Asp.net Core 2.0 Configure.Services <model> ()绑定不适用于用户机密 - Asp.net core 2.0 Configure.Services<model>() binding isn't working with user secrets Asp.net Core 1.1验证安全性标记不起作用 - Asp.net core 1.1 validate security stamp isn't working HTTP 错误:405(此页面不工作)当我刷新页面时(ASP.Net Core MVC) - HTTP ERROR : 405(This page isn't working) When I Refresh The Page(ASP.Net Core MVC) ASP.NET 核心:使用自定义身份选项不起作用 - ASP.NET Core: Using custom Identity Options isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM