Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 570
Kirsten Avatar asked Sep 17 '18 05:09

Kirsten


People also ask

How do you specify app settings for an Azure functions project?

To find the application settings, see Get started in the Azure portal. The Application settings tab maintains settings that are used by your function app. You must select Show values to see the values in the portal. To add a setting in the portal, select New application setting and add the new key-value pair.


1 Answers

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");
    
like image 166
Jerry Liu Avatar answered Oct 12 '22 15:10

Jerry Liu