ASP.NET Core 2.0 IHostEnvironment和IApplicationLifetime介绍

时间:2018-05-29 11:40:05   收藏:0   阅读:639

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");
    });
}

 

原文:https://www.cnblogs.com/lgxlsm/p/9104154.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!