我可以如何向Azure函数提供设置文件? [英] Can I, how do I, supply a settings file to an Azure Function?

查看:92
本文介绍了我可以如何向Azure函数提供设置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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'.

我尝试通过VS2017项目发布选项卡上的管理应用程序设置"链接来放入相关设置.显然,这失败了.还有其他方法可以使用吗?

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?

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

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;
    }
}

当我添加

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

我得到的功能

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

在门户网站中运行时

推荐答案

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

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. appsettings.json置于功能项目下,并将其设置为复制到输出/发布目录.

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

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

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

在Azure函数中传递appsettings.json的有效路径以初始化XeroApiSettings.

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

var xeroApiSettings = new XeroApiSettings(context.FunctionAppDirectory+"/appsettings.json");

这篇关于我可以如何向Azure函数提供设置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆