.Net Core 2 控制台应用程序配置文件转换

6

我正在尝试为 .net core 2 控制台应用程序添加 appsettings 转换,例如:

  • appSettings.json
  • appSettings.Test.json
  • appSettings.Prod.json

我发现以下代码适用于 asp.net core:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: 
true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: 
true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

然而,我不知道如何获取env.EnvironmentName,因为控制台应用程序中没有IHostingEnvironment

任何帮助将不胜感激。


可能是在ASP.NET5控制台应用程序中使用Startup类的重复问题。 - SpruceMoose
2
这不是重复的问题,因为IApplicationEnvironment已被移除。 https://github.com/aspnet/PlatformAbstractions/issues/37 - Aman B
2
Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application 不包含任何环境信息。 - Aman B
好观点,言简意赅。 - SpruceMoose
1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接