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

若如初见. 提交于 2019-12-08 19:34:19

问题


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


回答1:


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.

  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");
    



回答2:


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



来源:https://stackoverflow.com/questions/52361509/can-i-how-do-i-supply-a-settings-file-to-an-azure-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!