Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure functions: Read config from host.json after deploy

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

like image 829
Liquid Core Avatar asked Aug 16 '18 15:08

Liquid Core


People also ask

What is use of host json in Azure function?

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.

What is the host json file for?

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.


Video Answer


2 Answers

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.

enter image description here

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.

like image 163
Tom Sun - MSFT Avatar answered Oct 19 '22 00:10

Tom Sun - MSFT


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

like image 32
Duck Ling Avatar answered Oct 18 '22 22:10

Duck Ling