C# asp.net核心:IApplicationLifetime.ApplicationStopping isn';t触发

C# asp.net核心:IApplicationLifetime.ApplicationStopping isn';t触发,c#,asp.net-core,asp.net-core-middleware,C#,Asp.net Core,Asp.net Core Middleware,我看过一些关于IApplicationLifetime的文章,以及在应用程序启动和停止时触发执行的方法,但我可能错过了一些东西,因为它只是没有被触发。 以下是我的解决方法: 我为kubernetes(asp.net core 2.2)打开了一个项目模板容器应用程序 Program.cs看起来与创建时一样: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System

我看过一些关于IApplicationLifetime的文章,以及在应用程序启动和停止时触发执行的方法,但我可能错过了一些东西,因为它只是没有被触发。 以下是我的解决方法: 我为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>();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.Logging;
名称空间Kubernetes1
{
公共课程
{
公共静态void Main(字符串[]args)
{
CreateWebHostBuilder(args.Build().Run();
}
公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup();
}
}
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");
            });
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.Hosting.Internal;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.DependencyInjection;
名称空间Kubernetes1
{
公营创业
{
//此方法由运行时调用。请使用此方法将服务添加到容器中。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void Configure(IApplicationBuilder应用程序、IHostingEnvironment环境、IApplicationLifetime应用程序时间)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//var appLifetime=app.ApplicationServices.GetRequiredService();
appLifetime.ApplicationStopping.Register(()=>Console.WriteLine(“调用ApplicationStopping”);
appLifetime.ApplicationStopped.Register(()=>Console.WriteLine(“调用ApplicationStopped”);
app.Run(异步(上下文)=>
{
控制台写入线(“AAA”);
});
}
}
}
我想在这里做的就是在cmd中运行应用程序,然后停止它,并看到控制台中打印的两行内容:

  • 应用程序停止调用
  • 被调用的应用程序 我没能做到。 有什么想法吗

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

    停止进程
    将终止进程,跳过应用程序可能具有的任何正常关闭行为

    iaapplicationlifetime
    谈到应用程序停止时,它指的是应用程序正常关闭。根据应用程序的启动方式,有几种触发方法:

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

    检查输出时如何停止程序?在不同的cmd窗口中,我键入:
    Get Process-Name*dotnet*| stop Process
    在同一窗口中尝试
    cntrl+c
    。谢谢!事实上,这就是问题所在。我不知道停止过程不是一个优雅的停止我处于与上面类似的情况,但它不会工作。我正在运行IIS Express。我使用任务托盘“停止站点”,但我的应用程序停止事件从未命中。这可能不适用于IIS Express吗?