Jon*_*len 1 c# .net-core asp.net-core
这是我的启动代码:
services.Configure<FooBarOptions>(Configuration.GetSection("FooBarOptions"));
Run Code Online (Sandbox Code Playgroud)
这是一个错误,正如我想要的
services.Configure<FooBarOptions>(Configuration.GetSection("FooBar"));
Run Code Online (Sandbox Code Playgroud)
显然,拼写错误导致配置无法加载。当所需的配置部分不存在时,有没有办法在启动过程中触发错误?
将 aPostConfigure<T>与回调一起使用,该回调将在每个Configure<T>执行后运行。这将允许用户以任何方式、任何顺序配置选项,并允许您检查(未)配置的选项并在需要时引发异常。
class FooBarOptions
{
public string AString { get; set; }
}
services.PostConfigure<FooBarOptions>(options => {
// ... validate configuration
if (options.AString == null)
{
throw new InvalidProgramException($"{nameof(FooBarOptions)} is not configured properly");
}
});
services.Configure<FooBarOptions>(Configuration.GetSection("FooBarOptions"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
384 次 |
| 最近记录: |