假设我们有这样的 AppSettings.json
{
"Region": Europe,
"WeirdService": {
"JustField": "value"
}
}
Run Code Online (Sandbox Code Playgroud)
在单独的单例类中注册WeirdService设置(或使用选项模式)很好,只需:
service.AddSingleton(configuration.GetSection("WeirdService").Get<WeirdService>();
Run Code Online (Sandbox Code Playgroud)
此时就可以了。然而,我不知道如何像我的示例中那样干净地处理这个顶级属性Region。
我知道我可以直接注入IConfiguration和使用config.GetValue<string>("Region")或直接访问配置,但我想知道是否有一些干净、更好的方法,无需在服务中对这些东西进行硬编码。
我忘了提及。我目前合作的团队使用 .NET Core 3.1,因为它是当前的 LTS 版本。