【问题标题】:Add custom section to .NET Core local.settings.json将自定义部分添加到 .NET Core local.settings.json
【发布时间】:2019-11-15 15:40:58
【问题描述】:

我正在编写我的第一个 Azure 函数(基于 .NET Core 2.x),并且我正在努力使用我的自定义映射表扩展应用程序设置。

我有一个文件 local.settings.json,我尝试使用包含一些“键/值”对的“自定义”配置部分来扩展它 - 如下所示:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    ... (standard config settings) ...
  },
  ... (more default sections, like "Host" and "ConnectionStrings")
  "MappingTable" : {
    "01" : "Value1",
    "02" : "Value2",
    "03" : "Value3",
    "else" : "Value4"
  }
}

我通过构造函数注入将IConfiguration 注入到我的工作类中,它适用于存储在“值”部分中的“基本”默认值:

public MyWorker(IConfiguration config)
{
    _config = config;

    string runtime = _config["FUNCTIONS_WORKER_RUNTIME"];  // works just fine

    // this also works fine - the "customSection" object is filled
    var customSection = _config.GetSection("MappingTable");

    // but now this doesn't return any values
    var children = customSection.GetChildren();

    // and trying to access like this also only returns "null"
    var mapping = customSection["01"];
}

我被困在这里 - 我发现的所有博客文章和文章似乎都表明这样做 - 但就我而言,这似乎不起作用。我在这里想念什么?我对完整的 .NET 框架配置系统非常熟悉——但这对我来说是新的,而且还没有真正的意义......

我还尝试将整个 MappingTable 部分移动到 appSettings.json - 但这并没有改变任何东西,当我尝试访问我的自定义配置部分的值时,我仍然只返回 null... .

谢谢!

更新:使用标准 ASP.NET Core 2.1 Web 应用程序执行此操作的所有建议方法都可以正常工作 - 但在 Azure 函数中,它不起作用。似乎 Azure 函数 中的代码与常规 .NET Core 代码库相比以不同方式处理配置 ...

【问题讨论】:

  • 你为什么不用这个System.Environment.GetEnvironmentVariable($"MappingTable:01")
  • @NilayVishwakarma:试过这个 - 但它也只是返回“null”而不是配置的值......
  • 配置["MappingTable:01"]
  • @sjokkogutten:像以前一样——只返回null——好像.NET Core 配置系统看不到我的自定义部分.....
  • 嗯。您可以尝试将您的 customsection 包装在 AppSettings 部分中吗? "AppSettings":{"MappingTable": ...等}。然后: config["AppSettings:MappingTable:01"]

标签: configuration azure-functions asp.net-core-2.1


【解决方案1】:

我使用 .net core 3.0 做了类似的事情

local.settings.json

{
  "AppSettings":{
    "MappingTable" : {
      "01" : "Value1"       
    }
  }
}

阅读应用设置:

private void AppSettings()
{
   var config = new ConfigurationBuilder()
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                        .AddEnvironmentVariables()
                        .Build();

   var 01 = config["AppSettings:MappingTable:01"];
}

在 Azure 门户中,您需要将其添加为应用程序设置。 在您的功能应用程序中 -> 配置 - 应用程序设置 -> 新应用程序设置

Name:AppSettings:MappingTable:01
Value:Value1

【讨论】:

  • 看起来很有希望 - 但即便如此:我仍然只得到一个 null 回来 - 无论我尝试哪种设置.... .
  • 更新: 这一切都适用于标准 ASP.NET Core 2.1 Web 应用程序 - 但在 Azure 函数中,它不起作用。似乎 Azure 函数中的代码对配置的处理方式与常规 .NET Core 代码不同......
【解决方案2】:

A R G H ! ! !

我知道 - 这么愚蠢的小错误 - 但后果相当严重.....

在我的启动课上,我有这段代码(还有更多):

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddHttpClient();

        var config = new ConfigurationBuilder()
            .SetBasePath(Environment.CurrentDirectory)
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
        .....
    }
}    

但缺少的是上面调用.Build()之后的这一行代码:

        builder.Services.AddSingleton<IConfiguration>(config);

不知何故,local.settings.jsonValues 部分中的设置存在并可访问,但任何自定义配置部分都没有。

添加这一行解决了我的问题 - 现在我可以轻松地从我的 local.settings.json(或 appsettings.json)文件中读取 MappingTable 并在我的代码中使用它。

【讨论】:

  • 这是 .net cpre 2.1 还是 3.0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-07
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多