A:增加Server端使用示例
This commit is contained in:
1 parent
989747a2ae
commit
b14721d79e
23 files changed
+1411
-37
No files matched your search
@@ -36,6 +36,30 @@
|
||||
|
||||
## 使用方式
|
||||
|
||||
### 0) 约定优于配置的快速接入
|
||||
|
||||
```
|
||||
// 1 行代码拿到强类型配置,并做数据注解验证
|
||||
var settings = ConfigDefaults.Load<AppSettings>();
|
||||
|
||||
// 或者先构建 ConfigManager,方便后续使用 IConfiguration
|
||||
var manager = ConfigDefaults.BuildDefault(opt =>
|
||||
{
|
||||
opt.EnvironmentPrefix = "APP"; // 仅读取 APP__ 前缀的环境变量
|
||||
opt.AdditionalFiles.Add("extra.ini"); // 按需附加其它文件,默认 optional
|
||||
});
|
||||
var settings = manager.Get<AppSettings>(validate: true);
|
||||
```
|
||||
|
||||
依赖注入中也可以一步完成“构建+绑定+热更新”:
|
||||
|
||||
```
|
||||
services.AddDefaultConfigBinding<AppSettings>(defaults =>
|
||||
{
|
||||
defaults.EnvironmentName = "Production"; // 手动指定环境
|
||||
}, section: "App", onReload: s => logger.LogInformation("配置已更新"));
|
||||
```
|
||||
|
||||
### 1) 从文件构建(常见)
|
||||
|
||||
```
|
||||
@@ -183,4 +207,3 @@ var cm = provider.GetRequiredService<ConfigManager>();
|
||||
- 提供选项开关以切换 INI 映射策略(是否复制根键到各节)
|
||||
- 为 INI/TOML 增加自定义 `FileConfigurationProvider`,支持 `ReloadOnChange`
|
||||
- 增强序列化器的集合/字典类型支持(当前对 INI 做了简化)
|
||||
|
||||
Reference in new issue
Block a user