ASP.NET Core 2.0 IHostEnvironment和IApplicationLifetime介绍
Posted 积少成多
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()}"); }); }
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介绍的主要内容,如果未能解决你的问题,请参考以下文章