问题
I am writing an Azure function in VS 2017. I need to set up a few custom configuration parameters. I added them in local.settings.json
under Values
.
{
"IsEncrypted":false,
"Values" : {
"CustomUrl" : "www.google.com",
"Keys": {
"Value1":"1",
"Value2" :"2"
}
}
}
Now, ConfigurationManager.AppSettings["CustomUrl"]
returns null.
I'm using:
- .NET Framework 4.7
- Microsoft.NET.Sdk.Functions 1.0.5
- System.Configuration.ConfigurationManager 4.4.0
- Azure.Functions.Cli 1.0.4
Am I missing something?
回答1:
Firstly, I create a sample and do a test with your local.settings.json data, as Mikhail and ahmelsayed said, it works fine.
Besides, 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.
My Test:
ConfigurationManager.AppSettings["CustomUrl"]
returns null with the following local.settings.json.
{
"IsEncrypted": false,
"Values": {
"CustomUrl": "www.google.com",
"testkey": {
"name": "kname1",
"value": "kval1"
}
}
}
回答2:
Environment.GetEnvironmentVariable("key")
I was able to read values from local.settings.json using the above line of code.
回答3:
If you are using TimeTrigger based Azure function than you can access your key (created in local.settings.json) from Azure Function as below.
[FunctionName("BackupTableStorageFunction")]
public static void Run([TimerTrigger("%BackUpTableStorageTriggerTime%")]TimerInfo myTimer, TraceWriter log, CancellationToken cancellationToken)
回答4:
Azure function copies the binaries to the bin folder and runs using the azure function cli, so it searches for the local.settings.json, so make sure you have set the "Copy to Output Directory"
to "Copy Always"
回答5:
Hey you mmight be able to read the properties while debugging, but once you go and try to deploy that in azure, those properties are not going to work anymore. Azure functions does not allow nested properties, you must use all of them inline in the "Values" option or in "ConnectionStrings". Look at this documentation as reference: https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings
来源:https://stackoverflow.com/questions/46714677/read-values-from-local-settings-json-in-vs-2017-azure-function-development