Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publish azure functions and use appsettings.json

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.

like image 665
ObiEff Avatar asked Aug 14 '17 12:08

ObiEff


2 Answers

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

like image 197
Jan_V Avatar answered Sep 23 '22 07:09

Jan_V


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,

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

like image 30
Thilanka Ishara Gunathilake Avatar answered Sep 23 '22 07:09

Thilanka Ishara Gunathilake