【问题标题】:asp.net core: IApplicationLifetime.ApplicationStopping isn't triggeredasp.net 核心:未触发 IApplicationLifetime.ApplicationStopping
【发布时间】:2020-11-23 14:54:23
【问题描述】:

我看到了几篇关于 IApplicationLifetime 以及在应用程序启动和停止时触发执行的方式的文章,但我可能错过了一些东西,因为它只是没有被触发。 这是我的解决方法: 我打开了一个项目模板Container Application for kubernetes, asp.net core 2.2

Program.cs 看起来与创建时一样:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Kubernetes1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}

Startup.cs 如下所示:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Kubernetes1
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            //var appLifetime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
            appLifetime.ApplicationStopping.Register(() => Console.WriteLine("ApplicationStopping called"));
            appLifetime.ApplicationStopped.Register(() => Console.WriteLine("ApplicationStopped called"));
            app.Run(async (context) =>
            {
                Console.WriteLine("AAA");
            });
        }
    }
}

我在这里要做的就是在 cmd 中运行应用程序,然后停止它并在控制台中看到 2 行打印:

  1. ApplicationStopping 已调用
  2. ApplicationStopped 调用 我没能做到。 有什么想法吗?

【问题讨论】:

  • 检查输出时如何停止程序?
  • 在另一个 cmd 窗口中我输入:Get-Process -Name *dotnet* | Stop-Process
  • 在同一窗口中尝试cntrl+c
  • 谢谢!确实是问题所在。不知道停止过程不是优雅的停止

标签: c# asp.net-core asp.net-core-middleware


【解决方案1】:

在另一个 cmd 窗口中我输入:Get-Process -Name *dotnet* | Stop-Process

Stop-Process杀死进程,跳过应用程序可能具有的任何正常关闭行为。

IApplicationLifetime 谈到应用程序停止时,它指的是应用程序正在正常关闭。有几种触发方式,具体取决于应用程序的启动方式:

  • 当应用程序在控制台中运行时:CTRL + C。比如通过dotnet run运行或者直接运行编译后的可执行文件时。
  • 当应用程序在 IIS 中运行时:停止网站或应用程序池。
  • 当应用程序作为 Windows 服务运行时:停止服务。

【讨论】:

  • 我的情况与上述类似,但它不起作用。我在 IIS Express 中运行。我使用任务托盘“停止站点”,但我的应用程序停止事件从未被命中。这可能不适用于 IIS Express 吗?
猜你喜欢
  • 2020-04-09
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
  • 2016-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多