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.
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).
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 protected]",
"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,
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)
required, Azure CLI need be installed in your machine,
Download
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