Configuring environments in .NET console app

46 篇文章 0 订阅

对于非asp.net core程序发现环境变量读取始终是Production,配置基本和asp.net core程序一样了,读取的还是Production,参考如下文章才解决。

用  Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

直接读取环境变量。

ASP.NET Core applications are already set up with support for reading configuration files and switching between different environments. You get none of that when you create a new .NET console application. Fortunately, you can still take advantage of the same NuGet packages add similar support with minimum amount of setup code.

The configuration classes used by ASP.NET Core are available in NuGet packages prefixed by Microsoft.Extensions.Configuration. I installed the following ones for my setup:

With all these packages installed, I could initialize the ConfigurationBuilder with a subset of the default ASP.NET Core setup:

var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
    .AddJsonFile($"appsettings.json", true, true)
    .AddJsonFile($"appsettings.{environment}.json", true, true)
    .AddEnvironmentVariables();
var configurationRoot = builder.Build();

The above setup will read the configuration values from the appsettings.json file, the environment specific appsettings.*.json file, and the environment variables. The values from the source listed later will overwrite the values from a source listed earlier.

I decided to read the environment name from the same environment variable as ASP.NET Core does (i.e. ASPNETCORE_ENVIRONMENT), although the name isn't all that intuitive.

The following line will map the configuration to a strongly typed class:

var appConfig = configurationRoot.GetSection(nameof(AppConfig)).Get<AppConfig>();

The class must have properties that match individual configuration values to be read, e.g.:

public class AppConfig
{
    public string Environment { get; set; }
}

The above class code and class will map the values from the following configuration file:

{
  "AppConfig": {
    "Environment": "DEV"
  }
}

To switch between different environments in Visual Studio, you can configure profiles on the Debug tab of the Project Properties window:

I find it even more convenient to edit the profiles in the launchSettings.json placed in the Properties folder of the project:

{
  "profiles": {
    "EnvironmentConfiguration.Cli (dev)": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "EnvironmentConfiguration.Cli (prod)": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    }
  }
}

The file will not be present in a new console app project. But it will be added by Visual Studio as soon as you start editing the profiles in the Project Properties window.

Once you configure the profiles, you can select the profile to run from the Visual Studio toolbar:

To see a working example, you can check the code in my GitHub repository.

Although there's no out-of-the-box support for configuration in .NET console apps, you can add it yourself using the same classes as the default setup in ASP.NET Core apps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值