I've made an azure function in C#. I use local.settings.json for testing locally. Everything works correctly with
ConfigurationManager.Appsettings["key"]
Now I have published it. Nothing is working anymore. Host.json is there, I can browse the function app settings tab and I can see the configuration host.json right there with all the values.
Host.json format is the same as local.settings.json:
{
"IsEncrypted": false,
"Values": {
"MYCONFIG": "HEY",
"THEOTHERCONFIG" : "WHASSUP"
}
}
If I run locally everything is working fine. If I publish I get null in all the values.
I tried
ConfigurationManager.Appsettings["key"]
and
System.Environment.GetEnvironmentVariable("MYCONFIG", EnvironmentVariableTarget.Process);
Nothing works.
Microsoft documentation doesn't unveil the sacred secret of reading a config file.
Here I see they don't even mention a host.json file, they just say you have to manually put them in the azure portal.... which is higly impractical.
Any suggestion? Thank you
The host. json metadata file contains configuration options that affect all functions in a function app instance. This article lists the settings that are available starting with version 2. x of the Azure Functions runtime.
The host. json file is a configuration file containing the values that affect all the functions of a function app. This file is created as soon as we add an Azure Function project. This file will have at least one field to begin with, indicating the runtime version of Azure Functions.
Settings in the local.settings.json file are only used by Functions tools when running locally. By default, these settings are not migrated automatically when the project is published to Azure. We could use the Azure Functions Core Tools to publish the local.setting.json to Azure easily.
func azure functionapp publish azurefunctionname --publish-local-settings
The host.json metadata file contains global configuration options that affect all functions for a function app.
host.json is not for config Azure function appsettings.
Or as Thomas mentioned that you could config it in your appsettings blade of the azure function.
Update:
If you want to delegate some developement and testing you have to give them credentials... how ridiculous
You could use the Key vault and Azure function MSI to avoid sharing your credentials.
I appreciate that host.json
might not be there for configuring appsettings
, but just as an answer to the question...
As read in the docs:
When the runtime finds an application setting in the format AzureFunctionsJobHost__path__to__setting, it overrides the equivalent host.json setting
Example. You'd override the below host.json
file:
{
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true
}
}
}
}
With appsettings.json
/local.settings.json
:
{
...
"Values": {
"AzureFunctionsJobHost__logging__applicationInsights__samplingSettings__isEnabled":"false"
...
}
}
This means you can dynamically control what's in host.json this way. Not quite an answer to the OPs question, since it's not accessing but overriding, but I think it's a somewhat valid way to control what's in host.json
. If it's a good practice, or not - not me to decide :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With