【发布时间】: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