我可以如何向Azure函数提供设置文件?

Kir*_*eed 6 c# azure xero-api azure-functions

将使用设置文件的应用程序移植到Azure函数时,是否有必要消除对文件的依赖?

我想编写一个功能应用程序,以将数据从Xero导入到Azure sql数据库中。我正在使用的Xero SDK需要一个appsettings.json文件。

因此,当函数运行时,我得到了错误

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'.
Run Code Online (Sandbox Code Playgroud)

我尝试通过VS2017 Project Publish选项卡上的Manage Application Settings链接将相关设置放入。显然,这失败了。我还有其他方法可以使用吗?

这是api中的相关代码。我希望不必修改它,以便可以使用官方的nuget包。

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我添加

    log.LogInformation("base directory: "+AppDomain.CurrentDomain.BaseDirectory);
Run Code Online (Sandbox Code Playgroud)

我得到的功能

 D:\Program Files (x86)\SiteExtensions\Functions\2.0.12095-alpha\32bit\
Run Code Online (Sandbox Code Playgroud)

在门户网站中运行时

Jer*_*Liu 5

将使用设置文件的应用程序移植到 Azure Function 时,是否需要消除对文件的依赖?

没有必要,我们仍然可以使用应用程序所需的设置文件。我们只需要确保设置文件的路径是正确的。

  1. 放在appsettings.json函数项目下,设置复制到output/publish目录。

    设置文件属性

  2. ExecutionContext context在 Azure Function 方法签名中添加,用于查找当前函数应用目录(appsettings.json 所在的位置)。

  3. 在 Azure Function 中传递 appsettings.json 的有效路径以初始化 XeroApiSettings。

    var xeroApiSettings = new XeroApiSettings(context.FunctionAppDirectory+"/appsettings.json");
    
    Run Code Online (Sandbox Code Playgroud)