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

标签 c# configuration .net-core console-application .net-core-2.0

我想使用强类型类访问 appsettings.json 文件(可能还有其他配置文件)。 有很多关于在 .NET Core 1 中这样做的信息(例如 https://weblog.west-wind.com/posts/2016/may/23/strongly-typed-configuration-settings-in-aspnet-core ),但几乎没有关于 .NET Core 2 的信息。

此外,我正在构建一个控制台应用程序,而不是 ASP.NET。

似乎配置 API 在 .NET Core 2 中已完全改变。我无法解决。任何人?

编辑:我认为 Core 2 文档可能还没有跟上。示例:https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.configurationbinder?view=aspnetcore-2.0表示,您会认为,ConfigurationBinder 存在于 .NET Core 2 中,但 Microsoft.Extensions.Configuration 和 Microsoft.Extensions.Options 的对象浏览器搜索没有显示任何内容。

我已经使用 NuGet 控制台

  • 安装包 Microsoft.Extensions.Options - 版本 2.0.0
  • 安装包 Microsoft.Extensions.Configuration - 版本 2.0.0

最佳答案

感谢 Martin Ullrich 的观察,这导致了解决方案。这里有几件事在起作用:

  • 我对 DLL 级引用感到厌烦,因为旧式“引用”(与 .NET Core 中的新“依赖项”相反)隐藏了所有内容
  • 我假设安装 NuGet Microsoft.Extensions.Configuration 包会安装子命名空间 DLL。事实上,在 NuGet Gallery 中有许多用于这些的包(请参阅 here)。 NuGet 控制台不会告诉您具体获得了哪些 DLL,但您可以在安装后在解决方案浏览器中看到它们: enter image description here
    并且,在 API Browser 中搜索 ConfigurationBinder什么都不产生,我猜是因为它是扩展库的一部分。
  • Rick Strahl 确实在他的帖子中提到了它(在问题中有链接),但我仍然没有注意到它:例如,Bind 方法看起来很像 ConfigurationBinder 上的静态方法。但实际上它是一种扩展方法。因此,它会在安装 NuGet 包时神奇地出现。这使我们严重依赖文档,我还没有完全弄明白。

因此,总而言之,解决方案是:

  • 安装包 Microsoft.Extensions.Configuration.Binder - 版本 2.0.0
  • 安装包 Microsoft.Extensions.Configuration.Json-Version 2.0.0

第一个给出了 .Bind 方法,第二个给出了 .SetBasePath.AddJsonFile 方法。

一旦我完善了它,我将在一天左右的时间内将最终代码添加到这里。

编辑:

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;
}

请注意,上面的代码实际上是针对 YAML 配置文件的。您需要调整加载 YAML 的单行以使用 JSON。我没有测试过这些,但它们应该很接近:

JSON:

{
  "SourcePhoneNum": "+61421999999",

  "TargetPhones": [
    {
      "Name": "John Doe",
      "PhoneNumber": "+61421999888"
    },
    {
      "Name": "Jane Doe",
      "PhoneNumber": "+61421999777"
    }
  ]
}

YAML:

SourcePhoneNum: +61421999999
TargetPhones:
    - Name: John Doe
      PhoneNumber: +61421999888
    - Name: Jane Doe
      PhoneNumber: +61421999777

关于c# - .NET Core 2 控制台应用程序中的强类型配置设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46096191/

相关文章:

c# - 转换这些的最佳方法是什么?

php - implicit_flush 的 "serious performance implications"是什么?

c# - 无法连接另一台服务器上的Mysql

.net-core - DevOps 托管管道无法构建 .NET Core 2.2

c# - Nerd Dinner 模型是否使用最佳实践来处理对象?

c# - 返回子类而不是父类(super class) F#

visual-studio-2010 - 如何将Visual Studio 2010帮助(F1)从本地更改为在线?

MySQL:如何查看使用了哪些配置文件?

c# - 在 .NET 中寻找简单的规则引擎库

c# - "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."