将配置文件的部分加载到 net 5.0 中的字典中

发布于 2025-01-20 09:10:41 字数 1256 浏览 1 评论 0原文

我必须研究我不知道的未开发的Net 5.0的应用程序。

我想在配置文件中添加一个部分,然后将其加载到字典中。

这是配置文件中的一节:

{
  "MyApp": {
    "ServiceExceptions": [
        { "Key1": "Value1" },
        { "Key2": "Value2" }
    ]
  }
}

此代码加载配置:

public static IConfiguration GetConfiguration(string name)
{
    IConfiguration oConfiguration = null;
    string basePath = Path.GetDirectoryName(typeof(SharedHelper).Assembly.Location);
    string configPath = Path.Combine(basePath, "Config");

    string fileConfig = string.Format("{0}.{1}.json", name, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));

    oConfiguration = new ConfigurationBuilder()
                           .SetBasePath(configPath)
                           .AddJsonFile(fileConfig, optional: false, reloadOnChange: true)
                           .Build();
        return oConfiguration;
    }

这是加载以绑定字典的代码,但是myExceptions为null:

_Configuration = GetConfiguration("BD.Authentication.DWA");
Dictionary<string, string> myExceptions = null;
_Configuration.GetSection("MyApp:ServiceExceptions").Bind(myExceptions);
Debug.WriteLine(myExceptions.Count); // throws exception null reference

在某个地方加载了数据,但是我不知道如何拉出

I have to work on an application in net 5.0 not developed by me, that I don't know.

I would like to add a section in the config file and load it into a dictionary.

this is the section in config file:

{
  "MyApp": {
    "ServiceExceptions": [
        { "Key1": "Value1" },
        { "Key2": "Value2" }
    ]
  }
}

this code load the configuration:

public static IConfiguration GetConfiguration(string name)
{
    IConfiguration oConfiguration = null;
    string basePath = Path.GetDirectoryName(typeof(SharedHelper).Assembly.Location);
    string configPath = Path.Combine(basePath, "Config");

    string fileConfig = string.Format("{0}.{1}.json", name, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));

    oConfiguration = new ConfigurationBuilder()
                           .SetBasePath(configPath)
                           .AddJsonFile(fileConfig, optional: false, reloadOnChange: true)
                           .Build();
        return oConfiguration;
    }

this is the code to load to bind the dictionary, but myExceptions is null:

_Configuration = GetConfiguration("BD.Authentication.DWA");
Dictionary<string, string> myExceptions = null;
_Configuration.GetSection("MyApp:ServiceExceptions").Bind(myExceptions);
Debug.WriteLine(myExceptions.Count); // throws exception null reference

somewhere are loaded the data, but i dont know how to pull outenter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹微笑 2025-01-27 09:10:41

只需将配置文件移入:

{
  "MyApp": {
    "ServiceExceptions": 
        { "Key1": "Value1" },
        { "Key2": "Value2" }
  }
}

并检验值:

var serviceTypeExceptions = _Configuration.GetSection("MyApp:ServiceExceptions").Get<Dictionary<string, string>>();

Just move the config file in:

{
  "MyApp": {
    "ServiceExceptions": 
        { "Key1": "Value1" },
        { "Key2": "Value2" }
  }
}

and retrive the values:

var serviceTypeExceptions = _Configuration.GetSection("MyApp:ServiceExceptions").Get<Dictionary<string, string>>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文