Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read values from local.setting.json while debugging test

i don't seem to be able to read anything from this file in an azure functions when running or debugging a test, however it works fine when debugging the whole application locally.. can anyone explain why at all ?

{
  "IsEncrypted": false,
  "Values": {
    "xyz": 123
  }
}

var res = ConfigurationManager.AppSettings.Get("xyz");

ty..

my suspicion is that it is due to the 'debug' being initiated from another project (Test project), and the local.settings.json does not get bundled up with the project being tested ?

like image 853
m1nkeh Avatar asked Jan 17 '18 18:01

m1nkeh


People also ask

How do I debug Azure function locally?

Start debugging by attaching to a w3wp.exe remote process At least one breakpoint is setup. Our DLL compile and publish on Azure should be on Debug. The DLL version published on Azure should match the local version.

What is local settings JSON in Azure function?

settings. json file is an analogue of the app. config file in a conventional . Net application. This file contains the configuration settings for your application that can be published to App Settings in your Azure Function App environment.


2 Answers

I added the settings programatically to minify the chances that sensitive data reach version control.

class LocalSettings
{
    public bool IsEncrypted { get; set; }
    public Dictionary<string, string> Values { get; set; }
}

public static void SetupEnvironment()
{
    string basePath = Path.GetFullPath(@"..\..\..\MyAzureFunc");
    var settings = JsonConvert.DeserializeObject<LocalSettings>(
        File.ReadAllText(basePath + "\\local.settings.json"));

    foreach (var setting in settings.Values)
    {
        Environment.SetEnvironmentVariable(setting.Key, setting.Value);
    }
}
like image 155
sillo01 Avatar answered Sep 19 '22 08:09

sillo01


Your suspicion is spot on. Only the Azure Functions Runtime Host actually knows to read settings from that file and merge them into the overall AppSettings. When you run in a test project, the Azure Functions Runtime Host is not involved and therefore you don't get access to them.

The simplest way to solve this would be to reflect all the same setting keys/values into your test project's app.config file under the standard <appSettings> section.

like image 27
Drew Marsh Avatar answered Sep 18 '22 08:09

Drew Marsh