Access environment name in Program.Main in ASP.NET Core

懵懂的女人 提交于 2019-12-04 23:37:05

I think the easiest solution is to read the value from the ASPNETCORE_ENVIRONMENT environment variable and compare it with EnvironmentName.Development:

var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var isDevelopment = environment == EnvironmentName.Development;
HamedH

This is my solution (written for ASP.NET Core 2.1):

public static void Main(string[] args)
{
    var host = CreateWebHostBuilder(args).Build();

    using (var scope = host.Services.CreateScope())
    {
        var services = scope.ServiceProvider;
        var hostingEnvironment = services.GetService<IHostingEnvironment>();

        if (!hostingEnvironment.IsProduction())
           SeedData.Initialize();
    }

    host.Run();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!