add following packages
<ItemGroup>
<PackageReference Include="microsoft.extensions.configuration" Version="6.0.0" />
<PackageReference Include="microsoft.extensions.configuration.binder" Version="6.0.0" />
<PackageReference Include="microsoft.extensions.configuration.json" Version="6.0.0" />
<PackageReference Include="microsoft.extensions.dependencyinjection" Version="6.0.0" />
<PackageReference Include="microsoft.extensions.options" Version="6.0.0" />
</ItemGroup>
sample code
class Config
{
public string Name { get; set; }
public int Age { get; set; }
public Proxy Proxy { get; set; }
public override string ToString()
{
return $"name:{Name}, age:{Age}, proxy:{Proxy}";
}
}
class Proxy
{
public string Server { get; set; }
public int Port { get; set; }
public override string ToString()
{
return $"server:{Server}, port:{Port}";
}
}
class TestController
{
// IOptions: 只读配置,不追踪变化
// IOptionsSnapshot: 在scope内配置不会变,出了scope或进入新scope就会更新变化
// IOptionsMonitor: 追踪配置变化
private IOptionsSnapshot<Config> optConfig { get; }
public TestController(IOptionsSnapshot<Config> config)
{
optConfig = config;
}
public void Test()
{
System.Console.WriteLine(optConfig.Value);
}
}
从json文件读取
class Program
{
static void Main(string[] args)
{
ServiceCollection services = new ServiceCollection();
services.AddScoped<TestController>();
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddJsonFile("config.json", true, true);
IConfigurationRoot configRoot = builder.Build();
services.AddOptions()
.Configure<Config>(e => configRoot.Bind(e)); // 绑定Config对象
//.Configure<Proxy>(e => configRoot.GetSection("proxy").Bind(e)); // 也可以只绑定Proxy对象
using (ServiceProvider provider = services.BuildServiceProvider())
{
int count = 5;
while (count-- > 0)
{
using (IServiceScope scope = provider.CreateScope())
{
IServiceProvider sp = scope.ServiceProvider;
TestController test1 = sp.GetService<TestController>();
test1.Test();
}
System.Console.WriteLine($"press return to continue, {count} remaining...");
System.Console.ReadLine();
// change config value here, would reflect the changes in above Test() call.
}
}
}
}
从命令行读取
static void Main(string[] args)
{
ServiceCollection services = new ServiceCollection();
services.AddScoped<TestController>();
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddCommandLine(args); // 加入命令行支持
builder.AddEnvironmentVariables("prefix_"); // 加入环境变量支持,建议使用前缀,避免和其他系统冲突。
IConfigurationRoot configRoot = builder.Build();
services.AddOptions()
.Configure<Config>(e => configRoot.Bind(e)); // bind to Config
//.Configure<Proxy>(e => configRoot.GetSection("proxy").Bind(e)); // bind to Proxy part only.
using (ServiceProvider provider = services.BuildServiceProvider())
{
TestController test1 = provider.GetService<TestController>();
test1.Test();
}
}
// 命令行调用
dotnet run --name abc --age 22 --proxy:server abc.ccc --proxy:port 888
---------------------------
知道的更多,不知道的也更多
---------------------------
标签:
.net core
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具