//类似 global.ashx的application事件的实现
1-Startup类中
public void Configure(IApplicationBuilder app, IHostingEnvironment env,IApplicationLifetime applicationLifetime) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } applicationLifetime.ApplicationStopped.Register(() => { Console.WriteLine("ApplicationStopped"); }); applicationLifetime.ApplicationStopping.Register(()=> { Console.WriteLine("ApplicationStopping"); }); applicationLifetime.ApplicationStarted.Register(()=> { Console.WriteLine("ApplicationStarted"); }); }
如果启动时控制台会输出 ApplicationStarted
使用Ctrl+c停止时会输出
ApplicationStopping
ApplicationStopped