[英]Strongly Typed Configuration Settings in .NET Core 2 Console Application
I'd like to access the appsettings.json file (and perhaps other config files) using strongly typed classes. 我想使用强类型类访问appsettings.json文件(可能还有其他配置文件)。 There is a lot of info out there about doing so in .NET Core 1 (eg. https://weblog.west-wind.com/posts/2016/may/23/strongly-typed-configuration-settings-in-aspnet-core ), but next to nothing about .NET Core 2. 在.NET Core 1中有很多关于这样做的信息(例如https://weblog.west-wind.com/posts/2016/may/23/strongly-typed-configuration-settings-in-aspnet -core ),但几乎没有关于.NET Core 2的内容。
In addition, I'm building a console app, not ASP.NET. 另外,我正在构建一个控制台应用程序,而不是ASP.NET。
It appears that the configuration API has completely changed in .NET Core 2. I can't work it out. 似乎配置API在.NET Core 2中已经完全改变了。我无法解决它。 Anyone? 任何人?
EDIT: I think perhaps the Core 2 docs have not caught up yet. 编辑:我想也许Core 2文档尚未赶上。 Example: https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.configurationbinder?view=aspnetcore-2.0 indicates, you would think, that ConfigurationBinder exists in .NET Core 2, but an object browser search of Microsoft.Extensions.Configuration and Microsoft.Extensions.Options reveals nothing. 示例: https : //docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.configurationbinder?view= aspnetcore-2.0表示,您认为,ConfigurationBinder存在于.NET Core 2中,但是对象浏览器搜索Microsoft.Extensions.Configuration和Microsoft.Extensions.Options没有显示任何内容。
I have used the NuGet Console to 我使用过NuGet控制台
Thanks to Martin Ullrich for that observation, which led to the solution. 感谢Martin Ullrich的观察,这导致了解决方案。 There were several things at play here: 这里有几件事情在玩:
Microsoft.Extensions.Configuration
package would install the child namespace DLLs. 我假设安装NuGet Microsoft.Extensions.Configuration
包将安装子命名空间DLL。 In fact there are many packages for those in the NuGet Gallery (see here ). 事实上,NuGet Gallery中有很多软件包(见这里 )。 The NuGet console doesn't tell you what DLLs you're getting specifically, but you can see them in the Solution Browser once they're installed: NuGet控制台不会告诉您具体使用哪些DLL,但是一旦安装完毕,您就可以在解决方案浏览器中看到它们: So, in summary, the solution was to: 总而言之,解决方案是:
the first giving the .Bind
method, and the second the .SetBasePath
and .AddJsonFile
methods. 第一个给出.Bind
方法,第二个.SetBasePath
和.AddJsonFile
方法。
I'll add the final code here in a day or so once I perfect it. 一旦我完善它,我会在一天左右添加最终代码。
EDIT: 编辑:
public class TargetPhoneSetting {
public string Name { get; set; } = "";
public string PhoneNumber { get; set; } = "";
}
public class AppSettings {
public List<TargetPhoneSetting> TargetPhones { get; set; } = new List<TargetPhoneSetting>();
public string SourcePhoneNum { get; set; } = "";
}
public static AppSettings GetConfig() {
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddYamlFile("appsettings.yml", optional: false)
;
IConfigurationRoot configuration = builder.Build();
var settings = new AppSettings();
configuration.Bind(settings);
return settings;
}
Note that the code above is actually for a YAML configuration file. 请注意,上面的代码实际上是针对YAML配置文件的。 You will need to tweak the single line that load the YAML to use JSON. 您需要调整加载YAML以使用JSON的单行。 I haven't tested these, but they should be close: 我没有测试过这些,但它们应该接近:
JSON: JSON:
{
"SourcePhoneNum": "+61421999999",
"TargetPhones": [
{
"Name": "John Doe",
"PhoneNumber": "+61421999888"
},
{
"Name": "Jane Doe",
"PhoneNumber": "+61421999777"
}
]
}
YAML: YAML:
SourcePhoneNum: +61421999999
TargetPhones:
- Name: John Doe
PhoneNumber: +61421999888
- Name: Jane Doe
PhoneNumber: +61421999777
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.