Azure Function Read local.settings.json to object

自闭症网瘾萝莉.ら 提交于 2021-01-03 06:19:47

问题


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

   {
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "",
"Hello": "world"
 },
"ClientConfiguration": {
    "this": "that",
    "SubscriberEndpoint": "",
    "Username": "",
    "Password": "",
    "ObjectEndpoint": ""
 }
}

in my code i have

 var config = JsonConvert.DeserializeObject<myConnectionObject> (Environment.GetEnvironmentVariable("ClientConfiguration"));

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.

IF I add ClientConfiguration {} to the values like

..."Values" : { ... 
"Hello":"world",
"ClientCOnfiguration" : {above}
}

I end up with an error saying azurewebjobsstorage can't be found and 'func settings list' is just empty


回答1:


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.

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 .

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.

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>();

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);

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.




回答2:


AS far as I know, Values collection is expected to be a Dictionary, if it contains any non-string values, it can cause Azure function can not read values from local.settings.json. For more details, please refer to the blog




回答3:


If you don't want to instantiate a new ConfigurationBuilder, you can simply format your settings in another way. Your local.settings.json file would look like this:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "",
    "Hello": "world",
    "ClientConfiguration__this": "that",
    "ClientConfiguration__SubscriberEndpoint": "........",
    "ClientConfiguration__Username": "........",
    .....
 }
}

Please note that the double underscores are important, as it instructs the environment variables reader to take this as a property of the complex object ClientConfiguration. Your custom properties have to be located inside the Values object, otherwise they won't be injected as environment variables.



来源:https://stackoverflow.com/questions/52843564/azure-function-read-local-settings-json-to-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!