简体   繁体   English

在 .NET 核心控制台应用程序中将 appsettings.json 设置放入 class 类型的 object 中

[英]Getting appsettings.json settings into an object of class type in .NET Core console application

I wanted to pull the configuration from appsettings.json into an object of type ExchangeOptions .我想将配置从appsettings.json拉到ExchangeOptions类型的 object 中。 I know that configuration.Get<T>() works in ASP.NET Core, but I forgot the correct package for .NET Core Console application.我知道configuration.Get<T>()在 ASP.NET 核心中工作,但我忘记了 .NET 核心控制台应用程序的正确 package。 I currently have the following NuGet packages:我目前有以下 NuGet 包:

  • Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.FileExtensions Microsoft.Extensions.Configuration.FileExtensions
  • Microsoft.Extensions.Configuration.Json Microsoft.Extensions.Configuration.Json

The example below is pretty self explanatory.下面的示例非常不言自明。

appsettings.json appsettings.json

{
  "ExchangeConfiguration": {
    "Exchange": "Binance",
    "ApiKey": "modify",
    "SecretKey": "modify"
  }
}

Program.cs程序.cs

using Microsoft.Extensions.Configuration;
using System;

public class ExchangeOptions
{
    public Exchange Exchange { get; set; }
    public string ApiKey { get; set; }
    public string SecretKey { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        IConfiguration configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();
    
        // These work fine
        var exchange = configuration["ExchangeConfiguration:Exchange"];
        var apiKey = configuration["ExchangeConfiguration:ApiKey"];
        var secretKey = configuration["ExchangeConfiguration:SecretKey"];

        // This doesn't work, because I don't have the right NuGet package
        ExchangeOptions exchangeOptions = configuration.Get<ExchangeOptions>();
    
        Console.ReadKey();
    }
}

The correct package for configuration.Get<ExchangeOptions>() was Microsoft.Extensions.Configuration.Binder . configuration.Get<ExchangeOptions>()的正确 package 是Microsoft.Extensions.Configuration.Binder Thanks anyway!不管怎么说,还是要谢谢你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在控制台应用程序 (.net Core) 中将数据写入 appsettings.json? - How to write data to appsettings.json in a console application (.net Core)? 使用.NET Core中的appsettings.json设置继承 - Inheritance with appsettings.json settings in .NET Core 如何在 asp.net 核心控制台应用程序中的 class 程序中的 appsettings.json 中获取价值 - How to get value from appsettings.json in Program class in asp.net core console application 如何在 .NET Core 的 appSettings.json 上配置 IdentiyServer 设置? - How to configure IdentiyServer settings on appSettings.json of .NET Core? ASP.NET Core appsettings.json 未加载正确的设置 - ASP.NET Core appsettings.json not loading the correct settings .net 5,secrets.json,appsettings.json 和 Z3A580F142203677F1F0BC3089 应用程序设置 - .net 5, secrets.json, appsettings.json and Azure Application Settings .NET Core 3.1 从 appsettings.json 为控制台应用程序加载配置 - .NET Core 3.1 loading config from appsettings.json for console application 如何使用 .Net 核心控制台应用程序从 appsettings.json 读取部分? - How to read a section from appsettings.json using .Net core console application? 如何在使用 .NET Core 的控制台应用程序中从 appsettings.json 获取值? - How to get values from appsettings.json in a console application using .NET Core? appsettings.json 中的哨兵配置与 .Net Core 3 控制台应用程序中的 Serilog - Sentry configuration in appsettings.json with Serilog in .Net Core 3 Console App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM