我正在编写我的第一个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
注入到我的worker类中,并且对于“值”部分中存储的“基本”默认值也可以正常工作
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 Framework配置系统非常熟悉-但这对我来说是新的,还没有真正意义……
我还尝试将整个MappingTable
部分移至appSettings.json
-但这并没有什么改变,尝试访问我的自定义项时,我仍然只返回null
配置部分的值。...
谢谢!
更新:使用标准的ASP.NET Core 2.1 Web应用程序,所有建议的完成此工作的方式都很好-但是在Azure功能中,它不起作用。似乎 Azure函数中的代码与常规.NET Core代码库相比对配置的处理不同 ...