简体   繁体   English

键值对值问题 appsettings

[英]Keyvaluepair value issue appsettings

This is my appsettings这是我的应用设置

  {
    "Messages": {
        "ApiUrl": "demo",   
        "ApiKey": {
          "Key": "key",
          "Value": "1234"
        }
    }
  }

Model Class: Model Class:

public class CPSSettings
{
    public Messages Messages { get; set; } = null!;  
}
public class Messages
{
    public string ApiUrl { get; set; } = null!;
    public KeyValuePair<string?, string?> ApiKey{ get; set; }
} 
       

I am not getting values for _settings.Messages.ApiKey.Key and _settings.Messages.ApiKey.Value我没有得到_settings.Messages.ApiKey.Key_settings.Messages.ApiKey.Value的值

But receiving value for ApiUrl .但接收ApiUrl的价值。 I have issue with KeyValuePair not receiving a value我对 KeyValuePair 没有收到值有疑问

public testClient(IOptions<CPSSettings> options)
{  
    _settings = options.Value;
}

//...

if (_settings.Messages.ApiKey.Key is not null 
    && _settings.Messages.ApiKey.Value is not null)
{
    var s = _settings.Messages.ApiKey.Key;    // am getting null vaues in both
    var s1 = _settings.Messages.ApiKey.Value
}

The members of KeyValuePair are readonly . KeyValuePair的成员是只读的。 The framework is unable to set those values after initializing the struct初始化结构后,框架无法设置这些值

I would suggest you use another object type that you control.我建议您使用您控制的另一种 object 类型。 One that has modifiable members.具有可修改成员的一种。

For example例如

public class CPSSettings {
    public Messages Messages { get; set; } = null!;  
}

public class Messages {
    public string ApiUrl { get; set; } = null!;
    public ApiKey ApiKey{ get; set; } //<-- NOTE THE CHANGE
} 

public class ApiKey {
    public string? Key { get; set; }
    public string? Value { get; set; }
}

That way, when binding the settings, the framework can initialize and properly set the values of the members这样,在绑定设置时,框架可以初始化并正确设置成员的值

Removing CPSSettings would simplify the scenario.删除CPSSettings将简化场景。 See below for how to access configuration values, viaOptions pattern in .NET , using Messages :请参阅下文,了解如何使用Messages通过.NET 中的选项模式访问配置值:

  1. Ensure you have registered Messages within Startup.ConfigureServices(...) (example below).确保您已在Startup.ConfigureServices(...)中注册了Messages (示例如下)。
services.Configure<Messages>(Configuration.GetSection("Messages"));
  1. Receive IOptions<Messages> as testClient constructor parameter (example below).接收IOptions<Messages>作为testClient构造函数参数(下面的示例)。
public testClient(IOptions<Messages> options)
{
    _settings = options.Value;
}

try this尝试这个

var cpsSettingsSection = configuration.GetSection("Messages");
var cpsSettings = cpsSettingsSection.Get<Messages>();

var url = cpsSettings.ApiUrl;
var key = cpsSettings.ApiKey.Key.Dump();    
var value = cpsSettings.ApiKey.Value.Dump();

//services.Configure<Messages>(cpsSettingsSection);

}

public class Messages
{
    public string ApiUrl { get; set; }
    public KeyValueString ApiKey {get; set;}
 }

public class KeyValueString
{
    public string Key { get; set; }
    public string Value { get; set; }
}

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

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