命令行方式配置
- nuget安装 Microsoft.Extensions.Configuration.CommandLine
- configBuilder.AddCommandLine(args)
- 参数支持多种格式,比如: server = 127.0.0.2、–server =127.0.0.2、–server 127.0.0.2(注意键值之间加空格)、/server =127.0.0.2、/server 127.0.0.2(注意键值之间加空格)。格式不能混用
- 调试的时候,vs中简化命令行传参数的方法。
代码演示
代码:
Program 类
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace 配置系统学习_3;
class Program
{
static void Main(string[] args)
{
ServiceCollection services = new ServiceCollection();
services.AddScoped<TestController>();
ConfigurationBuilder builder = new ConfigurationBuilder();
// 从命令行读取
builder.AddCommandLine(args);
// 最重要
IConfigurationRoot root = builder.Build();
services.AddOptions()
.Configure<Config>(e => root.Bind(e));
using (ServiceProvider sp = services.BuildServiceProvider())
{
TestController controller = sp.GetRequiredService<TestController1>();
controller.Test();
}
}
}
class Proxy
{
public string Address { get; set; }
public int Port { get; set; }
public string Ids { get; set; }
}
class Config
{
public string Name { get; set;
public int Age { get; set; }
public Proxy Proxy { get; set; }
}
using System;
using Microsoft.Extensions.Options;
namespace 配置系统学习_3
{
class TestController
{
private readonly IOptionsSnapshot<Config> optionsSnapshot;
public TestController(IOptionsSnapshot<Config> optionsSnapshot)
{
this.optionsSnapshot = optionsSnapshot;
}
public void Test()
{
Console.WriteLine("name: " + this.optionsSnapshot.Value.Name);
Console.WriteLine("age: " + this.optionsSnapshot.Value.Age);
}
}
}
我们可以通过命令行窗口进行赋值,具体方法是:打开命令行窗口–>切换到exe程序所在的目录下–>输入exe的名称 变量的值。以我的程序为例, …> 配置学习_3.exe name = haha age =18
由于我的电脑是mac,暂时没搞明白应该怎么用cmd窗口演示,这里不做演示了。
我们还可以在vs中直接配置命令行参数。
windows系统:在项目上右键–>属性—>调试—>应用程序参数框中输入即可。
mac:在项目上右键–>属性—>运行—>default—>参数
结果
接下来我们改一下testcontroller代码:
using System;
using Microsoft.Extensions.Options;
namespace 配置系统学习_3
{
class TestController
{
private readonly IOptionsSnapshot<Config> optionsSnapshot;
public TestController(IOptionsSnapshot<Config> optionsSnapshot)
{
this.optionsSnapshot = optionsSnapshot;
}
public void Test()
{
Console.WriteLine("name: " + this.optionsSnapshot.Value.Name);
Console.WriteLine("age: " + this.optionsSnapshot.Value.Age);
Console.WriteLine("port: " + this.optionsSnapshot.Value.Proxy.Port);
Console.WriteLine("address: " + this.optionsSnapshot.Value.Proxy.Address);
Console.WriteLine("ids: " + this.optionsSnapshot.Value.Proxy.Ids);
}
}
}
这时如果运行程序,程序会报错,因为我们没有配置proxy的值。下面我们来配置proxy
扁平化配置
对于环境变量、命令行等简单的键值对结构,如果想要进行复杂结构的配置,需要进行“扁平化处理”。对于配置的名字需要采用“层级配置”。例如:a : b : c 。对于数字这样的配置:a : b : c : 0、 a : b : c : 1、 a : b : c : 2
这个知识点在自定义配置Provider中还会用到
演示一下:
框内的内容如下:
name=aaa age=18 proxy:address = hahah proxy:port = 80 proxy:ids:0 = 000 proxy:ids:1 = 111
环境变量方式配置
- nuget安装: Microsoft.Extensions.Configuration.EnvironmentVariables
- configrationBuilder.AddEnvironmentVariables(). AddEnvironmentVariables()有无参数和有prefix参数的两个重载版本。无参数版本会把程序相关的所有环境变量都加载进来,由于可能和系统中已有的环境变量冲突,因此建议有prefix参数的。读取配置的时候,prefix参数会被忽略。
- vs调试中,避免修改系统环境变量,直接在vs中设置环境变量即可。
配置方法如下图所示:
Main方法只需要改一行代码
static void Main(string[] args)
{
ServiceCollection services = new ServiceCollection();
services.AddScoped<TestController>();
ConfigurationBuilder builder = new ConfigurationBuilder();
// 从命令行读取
//builder.AddCommandLine(args);
// 从环境变量读取
builder.AddEnvironmentVariables("M_");
// 最重要
IConfigurationRoot root = builder.Build();
services.AddOptions()
.Configure<Config>(e => root.Bind(e))
.Configure<Proxy>(e => root.GetSection("proxy").Bind(e));
using (ServiceProvider sp = services.BuildServiceProvider())
{
TestController controller = sp.GetRequiredService<TestController>();
controller.Test();
}
}
由代码和上图可知,我们加了M_前缀,这是为了和系统变量区分开。
注意:我们调试时可以安装这种方法设置环境变量,但是部署时,还是要去手动添加windows环境变量的。
代码和设置环境变量的方法就是这样的,没有任何错误。但是在我的电脑上运行报错,所以无法演示结果。
我的电脑是mac,网上查了下,说是冒号的原因,将冒号改成|能解决。我试了下,改成|的确不报错了,但是仍然读不到proxy的值。暂时没找到原因。