Sudama Tripathi
Sudama Tripathi

Reputation: 379

Could not load the appsettings.json file when function deployed to azure

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?

Upvotes: 1

Views: 2321

Answers (2)

Sudama Tripathi
Sudama Tripathi

Reputation: 379

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.

Upvotes: 0

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35154

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.

Upvotes: 5

Related Questions