ASP.NET Core 2.0 IHostEnvironment和IApplicationLifetime介绍

少年恃险若平地,独倚长剑凌清秋。这篇文章主要讲述ASP.NET Core 2.0 IHostEnvironment和IApplicationLifetime介绍相关的知识,希望能为你提供帮助。
IHostEnvironment获取程序信息

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {app.Run(async (context) => { await context.Response.WriteAsync($"ApplicationName:{env.ApplicationName}"); await context.Response.WriteAsync($"ContentRootPath:{env.ContentRootPath}"); await context.Response.WriteAsync($"WebRootPath:{env.WebRootPath}"); await context.Response.WriteAsync($"是否开发环境:{env.IsDevelopment()}"); }); }

ASP.NET Core 2.0 IHostEnvironment和IApplicationLifetime介绍

文章图片

IApplicationLifetime站点启动或关闭时的监控
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime applicationLifetime) { applicationLifetime.ApplicationStarted.Register(() => { Console.WriteLine("ApplicationStarted"); }); applicationLifetime.ApplicationStopped.Register(() => { Console.WriteLine("ApplicationStopped"); }); applicationLifetime.ApplicationStopping.Register(() => { Console.WriteLine("ApplicationStopping"); }); }

【ASP.NET Core 2.0 IHostEnvironment和IApplicationLifetime介绍】 

    推荐阅读