7

I have an Azure Functions project, which I am writing in C# using Visual Studio 2017 15.3 preview 7. The functions project contains two seperate functions, both of which are timer triggered. I have a number of appsettings which I have specified in an appsettings.json file, which is set to Copy to the output directory on build.

When I publish the Azure Functions project, the two functions are displayed in the Azure portal, however they fail as they do not have access to the app settings. Now I know I can specifiy the settings in the application settings blade, but I want to use appsettings.json which should be possible according to this github issue: https://github.com/Azure/azure-functions-cli/issues/33

This SO answer contains some information about getting it to work Azure Functions - using appsettings.json. Now the issue lies in "Azure will try looking for after appsettings.json file into your root folder of the Function that you working on." My function project as it stands contains the following file structure:

-appsettings.json
-Function_function1.cs
-Function_function2.cs
-host.json

When the project is built and deployed, I end up with a folder for each of the functions in the wwwroot, each of these contains a function.json file. The appsettings.json file goes into the wwwroot/bin folder and the functions are not able to access it.

I thought that maybe if I changed functions project to have a folder structure it would deploy the appsettings.json, I changed it to the following

-host.json
-Function_function1\
--Function_function1.cs
--appsettings.json
-Function_function2\
--Function_function2.cs
--appsettings.json

This didn't have the result I expected and at this point I am not quite sure what I need to do to get appsettings copied into the function folders under the wwwroot rather than the bin folder, any guidance will be appreciated.

ObiEff
  • 640
  • 8
  • 24

2 Answers2

6

The SO post you are referring to contains deprecated information. The appsettings.json isn't used anymore.

For local development you can use the local.settings.json file, but this isn't used in the Functions App. I just finished testing this and verified this claim.

You can use the --publish-local-settings flag when publishing your function in order to get the settings in your function app. Still, it's not something you want of course.

I myself am also still looking for a solution which doesn't involve creating ARM templates, but I got a feeling this isn't possible at the moment. You can of course write your own PowerShell scripts and set the Application Settings this way, but that's even worse.

Probably not really the answer you were looking for, but it's the best there is (at the moment).

Jan_V
  • 4,244
  • 1
  • 40
  • 64
  • 1
    This is correct I believe - we have gone down this path, using an ARM template to populate the app settings for our function apps. Then setting up VSTS build to update those apps as we commit changes to the code for the actual functions. It works well enough for us - but agree an alternative would be good, so keen to hear if there is one. – Garth Mason Aug 15 '17 at 02:01
  • 1
    Quite a shame that things have changed and it doesn't automatically use the appsettings.json. Another ways you should be able to do it is using the Azure Powershell commandlet "Set-​Azure​Website" and set -appsettings, this would then store the settings you specify in the standard app settings section, though hooking up the set-azurewebsite to pick up the appsettings.json would need more work. – ObiEff Aug 15 '17 at 08:52
  • 1
    Here's a GitHub feature request that you can share comments on regarding a simpler way to manage Function App settings: **https://github.com/Azure/azure-functions-cli/issues/339** That is a follow-on to the previous update to the Azure Functions CLI that gave us the ability to deploy _only_ the settings (and not the binaries): https://github.com/Azure/azure-functions-cli/issues/157 – Jonathan B. Dec 24 '17 at 23:15
  • Why do you say "Still, it's not something you want of course" in regards to `--publish-local-settings`? – xr280xr Aug 29 '23 at 20:45
  • @xr280xr, of course it depends on the scenario. It's perfectly fine to do this for side-projects, or something similar. But in production software you want these settings to be added/modified using your release pipeline (like via IaC), so no human is involved changing those settings and they aren't stored in source control. – Jan_V Aug 30 '23 at 05:43
3

In Azure Functions, setting are stored in local.setting.json (create this file if doesn't exist in your solution/ name should be exact as mentioned).

sample local.settings.json body,

{
  "IsEncrypted": false,
  "Values": {
    "email": "email@server.com",
    "pass": "pass"
  },
  "ConnectionStrings": {
    "SqlConnectionString": "server=jongdb;user=jong;"
  }
}

once you add setting file, you have to configure it under your Run() method as mentioned bellow,

enter image description here

var config = new ConfigurationBuilder().SetBasePath(context.FunctionAppDirectory)
                .AddJsonFile("local.settings.json", optional: false, reloadOnChange: true).AddEnvironmentVariables().Build();

When Accessing setting, use below

IConfigurationRoot config;
config["fromEmail"];

use below command to publish settings

func azure functionapp publish *YourAppName* --publish-local-settings -i

once the deployemnt complate, you'll see the setting in Azure portal, In your function , under Application Settings (check below image)

enter image description here

required, Azure CLI need be installed in your machine,

Download

  • In VS Code, if you have the Azure extension, you can simply right click on your function in the navigation pane and upload local settings. – adrien May 24 '19 at 05:28
  • You probably want that optional:false to be true as the local settings file shouldn't be in the output/function folder by design unless on local development machine. So wouldn't it fail in azure otherwise? The --publish-local-settings flag will send the development app settings to azure as production values, which get picked up by the .AddEnvironmentVariables() call you include. – Tyeth Mar 31 '21 at 14:37