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介绍的主要内容,如果未能解决你的问题,请参考以下文章

带你做 WebAPI 迁移 ASP.NET Core 2.0

ASP.Net Core 2.0 FileTable 使用

ASP.NET Core 2.0 中的多个身份

ASP.NET Core 2.0 : 八.图说管道

ASP.NET CORE 2.0 中的 FromUri

asp.net core 2.0 Json结果的格式