Can I, how do I, supply a settings file to an Azure Function?
Asked Answered
H

2

6

When porting an application that uses a settings file to an Azure Function, is it necessary to remove reliance on the file?

I want to write a function app to import data from Xero into an Azure sql database. The Xero SDK I am using is expecting an appsettings.json file.

Consequently when the function runs I get the error

System.Private.CoreLib: Exception while executing function:
FunctionXeroSync. Xero.Api: The type initializer for 
'Xero.Api.Infrastructure.Applications.Private.Core' threw an exception. 
Microsoft.Extensions.Configuration.FileExtensions: The configuration file 
'appsettings.json' was not found and is not optional. The physical path is 
'C:\Users\kirst\AppData\Local\AzureFunctionsTools\Releases\2.6.0\cli\appsettings.json'.

I tried putting the relevant settings in via the Manage Application Settings link on the VS2017 Project Publish Tab. Clearly this fails. Is there another way I can use?

Here is the relevant code in the api. I would prefer not to have to modify it, so that I can use the official nuget package.

namespace Xero.Api
{
    public class XeroApiSettings : IXeroApiSettings
    {
        public IConfigurationSection ApiSettings { get; set; }

        public XeroApiSettings(string settingspath)
        {

            var builder = new ConfigurationBuilder()
                .AddJsonFile(settingspath)
                .Build();

            ApiSettings = builder.GetSection("XeroApi");
        }
        public XeroApiSettings() : this("appsettings.json")
        {
        }

        public string BaseUrl => ApiSettings["BaseUrl"];

        public string CallbackUrl => ApiSettings["CallbackUrl"];

        public string ConsumerKey => ApiSettings["ConsumerKey"];

        public string ConsumerSecret => ApiSettings["ConsumerSecret"];

        public string SigningCertificatePath => ApiSettings["SigningCertPath"];

        public string SigningCertificatePassword => ApiSettings["SigningCertPassword"];

        public string AppType => ApiSettings["AppType"];

        public bool IsPartnerApp => AppType?.Equals("partner", StringComparison.OrdinalIgnoreCase) ?? false;
    }
}

When I add

    log.LogInformation("base directory: "+AppDomain.CurrentDomain.BaseDirectory);

to the function I get

 D:\Program Files (x86)\SiteExtensions\Functions\2.0.12095-alpha\32bit\

when running in the portal

Heartache answered 17/9, 2018 at 5:58 Comment(2)
#36974498Heartache
I ask a related question at #52398160Heartache
M
6

When porting an application that uses a settings file to an Azure Function, is it necessary to remove reliance on the file?

Not necessary, we can still use the settings file required by the application. We only need to make sure the path of settings file is correct.

  1. Put appsettings.json under function project and set it to be copied to output/publish directory.

    set file property

  2. Add ExecutionContext context in Azure Function method signature, it's used to find current function app directory(where appsettings.json locates).

  3. Pass the valid path of appsettings.json in Azure Function to initialize XeroApiSettings.

    var xeroApiSettings = new XeroApiSettings(context.FunctionAppDirectory+"/appsettings.json");
    
Matney answered 19/9, 2018 at 9:19 Comment(0)
G
3

This Jon Gallant's blog suggests that you need to add the optional parameter to the AddJsonFile as it does not exist when you deploy:

var config = new ConfigurationBuilder()
    .SetBasePath(context.FunctionAppDirectory)
    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

Note that in Azure, this will refer to the 'appsettings.json' file

Glassman answered 19/9, 2018 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.