简体   繁体   English

.NET Core 2控制台应用程序中的强类型配置设置

[英]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控制台

  • Install-Package Microsoft.Extensions.Options -Version 2.0.0 安装包Microsoft.Extensions.Options -Version 2.0.0
  • Install-Package Microsoft.Extensions.Configuration -Version 2.0.0 安装包Microsoft.Extensions.Configuration -Version 2.0.0

Thanks to Martin Ullrich for that observation, which led to the solution. 感谢Martin Ullrich的观察,这导致了解决方案。 There were several things at play here: 这里有几件事情在玩:

  • i'm rusty with DLL-level references because the old style 'references' (as opposed to the new 'dependencies' in .NET Core) hid all that 我对DLL级别的引用很生疏,因为旧样式的“引用”(与.NET Core中的新“依赖”相反)隐藏了所有这些
  • i was assuming that installing the NuGet 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,但是一旦安装完毕,您就可以在解决方案浏览器中看到它们: 在此输入图像描述
    AND, searching for ConfigurationBinder in the API Browser yields nothing, I'm guessing because it's part of the extension library. 并且,在API浏览器中搜索ConfigurationBinder不会产生任何结果,我猜是因为它是扩展库的一部分。
  • Rick Strahl did mention it in his post (linked in the question), but I still missed it: the Bind method, for example, looks a lot like a static method on ConfigurationBinder. Rick Strahl确实在帖子中提到了它(在问题中链接),但我仍然错过了它:例如,Bind方法看起来很像ConfigurationBinder上的静态方法。 However in reality it's an extension method. 但实际上它是一种扩展方法。 As such, it magically appears when the NuGet package is installed. 因此,在安装NuGet包时会神奇地出现。 This makes us rely heavily on the docs, which I haven't quite worked out yet. 这使得我们严重依赖于文档,我还没有完成。

So, in summary, the solution was to: 总而言之,解决方案是:

  • Install-Package Microsoft.Extensions.Configuration.Binder -Version 2.0.0 安装包Microsoft.Extensions.Configuration.Binder -Version 2.0.0
  • Install-Package Microsoft.Extensions.Configuration.Json-Version 2.0.0 安装包Microsoft.Extensions.Configuration.Json-Version 2.0.0

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM