1

I am working on azure function app which references another project from core folder and that project contains the below class.

In this I am trying to read appsettings.json file and it works fine locally but when it's deployed to azure portal it could not find this file and builder.Build() method throws FileNotFoundException.

public static class ConfigurationSettings
{
    public static IConfigurationRoot GetConfigurationRoot()
    {

        var builder = new ConfigurationBuilder()
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("appsettings.json");

        return builder.Build();
    }
}

Can anyone suggest me what wrong i am doing here and is there any other to include files in azure function apps?

3
  • Not an anser, but more of a tip. Don't use an appsettings.json file to load configuration values for your application. Use the proper functionality which is available, like the ConfigurationManager and use Application Settings, which are set either manual or via some deployment tool (like VSTS) Commented Dec 13, 2017 at 7:44
  • I created a custom provider after that i started getting the error "The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'D:\Program Files (x86)\SiteExtensions\Functions\1.0.11388\appsettings.json'." it's loooking for the file from where the code is being executed in the azure portal but i think it look for the file in the content root. Had it been normal .net core project i could achieve using IhostingEnvironment but not sure it will work in case of function app. Commented Dec 13, 2017 at 8:59
  • @Jan_V I know we should not use appsettings.json file but the thing is my project doesn't have any start up class and to bind some project settings this is the only way i am able to think right now. Commented Dec 13, 2017 at 9:02

2 Answers 2

5

When Functions execute in Azure, GetCurrentDirectory is set to D:\Windows\system32. There's no appsettings.json in that folder, thus the exception.

I hope you are able to set the directory from which the library is loading settings. If so, you should add an extra parameter to your function of type ExecutionContext and use its FunctionDirectory property. See docs.

Of course, be sure to publish appsettings.json to Azure as part of your deployment.

Sign up to request clarification or add additional context in comments.

3 Comments

appsettings.json is part of my function app which execute the run method but I am loading appsettings.json file from another class library. Once I published the functional app to azure all files and folders are getting copied including all the references, I can see files if I download content from portal but i still don't know what could be the path of appsettings.json once this get copied to azure and I am not sure if I can set some fix path for this file.
I think I got it, your answer worked for me just passed one more parameter ExecutionContext to run method and then I used FunctionDirectory property to retrieve file location.
If you copy the appsettings.json file, you should use FunctionAppDirectory property
0

I am now able to run the function app and read the appsettings.json file. here is how i achieved this:

Created Custom Configuration provider CustomConfigurationProvider, assigned type to static property SettingsType and also embedded appsettings.json file in the class library project:

public class CustomConfigurationProvider : ConfigurationProvider, IConfigurationSource
{
    public static Type SettingsType;
    public override void Load()
    {
        var JsonString = GetResponse("appsettings.json");
        var jsonObject =  JObject.Parse(JsonString);
        foreach (var settingsGroup in jsonObject)
        {
            var settingsGroupName = settingsGroup.Key;
            foreach (var setting in settingsGroup.Value)
            {
                var jProperty = setting as JProperty;
                Data.Add($"{settingsGroupName}:{jProperty.Name.ToString()}", jProperty.Value.ToString());
            }
        }
    }

    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return this;
    }

    protected string GetResponse(string resourceName)
    {
        var assembly = SettingsType.Assembly;
        var resourceStream = assembly.GetManifestResourceStream(SettingsType.Namespace+'.'+ "appsettings.json");
        using (var reader = new StreamReader(resourceStream, Encoding.UTF8))
        {
            return reader.ReadToEnd();
        }
    }
}

it is working for me but still looking for some better ways to do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.