Azure函数将local.settings.json读取为对象 [英] Azure Function Read local.settings.json to object
问题描述
我知道我可以在local.settings.json的值{}部分下添加所有环境变量.但是,我正在努力保持整洁的家,并希望我能做这样的事情.
I know i could add all my environment vars under the values {} section of local.settings.json. I am however trying to keep a tidy home and would like if I could do something like this.
local.settings.json
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "",
"Hello": "world"
},
"ClientConfiguration": {
"this": "that",
"SubscriberEndpoint": "",
"Username": "",
"Password": "",
"ObjectEndpoint": ""
}
}
在我的代码中,我有
var config = JsonConvert.DeserializeObject<myConnectionObject> (Environment.GetEnvironmentVariable("ClientConfiguration"));
无论我做什么,我都无法使它正常工作.为什么我至少不能获得ClientConfiguration的内容?只是不断返回null.
No matter what I do I cannot get this to work. Why can't I at least get the contents of ClientConfiguration? Just keeps coming back null.
如果我将ClientConfiguration {}添加到类似
IF I add ClientConfiguration {} to the values like
..."Values" : { ...
"Hello":"world",
"ClientCOnfiguration" : {above}
}
我最终收到一条错误消息,指出找不到azurewebjobsstorage,并且功能设置列表"为空
I end up with an error saying azurewebjobsstorage can't be found and 'func settings list' is just empty
推荐答案
对于 local.settings.json
,只能将 Values
部分导入到环境变量中.(如果您的函数是v2,环境变量中也有 ConnectionStrings
部分).因此,您会看到结果为null.
For local.settings.json
, only Values
section can be imported into Environment variables.(If your function is v2, ConnectionStrings
section is also there in Environment variables). So you see null as the result.
此外, Values
部分是 Dictionary< string,string>
,这意味着value除了字符串以外不能采用其他格式.因此,您的 ClientCOnfiguration
内部错误.
What's more, Values
section is a Dictionary<string, string>
which means value can't be in other format except string. Hence your ClientCOnfiguration
inside results in error .
由于您要重新组织这些设置,因此序列化 ClientConfiguraiton
并将其存储在 Values
中似乎不是一个好选择.我们可能只需要读取和解析Json文件即可.
Since you want to reorganize those settings, serializing ClientConfiguraiton
to store it in Values
seems not a good option. We may need simply reading and parsing Json file.
在函数方法签名中添加 ExecutionContext上下文
,然后尝试下面的代码.
Add ExecutionContext context
in your function method signature and try code below.
var reader = new StreamReader(context.FunctionAppDirectory+"/local.settings.json");
var myJson = reader.ReadToEnd();
dynamic config = JsonConvert.DeserializeObject(myJson);
var clientConfiguration = config.ClientConfiguration as JObject;
myConnectionObject mco = clientConfiguration.ToObject<myConnectionObject>();
如果您的函数是v2,则使用 ConfigurationBuilder
还有另一种方法.
If your function is v2, there's another way with ConfigurationBuilder
.
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var mco = new myConnectionObject();
config.GetSection("ClientConfiguration").Bind(mco);
请注意, local.settings.json
适用于本地开发人员,默认情况下不会上载到Azure.需要删除 functionname.csproj
中的< CopyToPublishDirectory>从不</CopyToPublishDirectory>
.
Note that local.settings.json
is for local dev, it won't be uploaded to Azure by default. Need to remove <CopyToPublishDirectory>Never</CopyToPublishDirectory>
in functionname.csproj
.
这篇关于Azure函数将local.settings.json读取为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!