Azure Functions,如何拥有多个 .json 配置文件 [英] Azure Functions, how to have multiple .json config files

查看:30
本文介绍了Azure Functions,如何拥有多个 .json 配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写了一个可以在本地正常工作的 azure 函数.我相信这归结为拥有 local.setting.json 文件.但是当我将它发布到 azure 时,该功能不起作用,因为它找不到我定义的设置值.来自 Web 应用程序和控制台驱动的方法,我们将拥有与每个环境相关联的不同配置文件.我怎样才能让它工作,这样我就可以有多个 settings.json 文件,例如一个用于开发、测试和生产环境?最终结果是使用 octopus deploy 来部署它,但在这一点上,如果我什至无法使用发布来让它工作,那么就没有机会这样做了.

So I have written an azure function that works locally just fine. I believe this is down to having the local.setting.json file. But when I publish it to azure the function does not work as it can't find the settings values that I had defined. Coming from a web app and console driven approach, we would have different config files that would be associated with each environment. How can I get this to work so I can have multiple settings.json files, e.g. one for dev, stag and prod environments? The end result is to get this deployed with octopus deploy, but at this point, if I cant even get it working with a publish then there is no chance of doing this.

我很困惑为什么这些信息不容易获得,因为我认为这是一件很常见的事情?

I'm rather confused why this information is not easily available, as would presume it is a common thing to do?

推荐答案

好的,我现在可以使用了 :) 因为我们使用 octopus deploy 我们不需要多个配置文件,所以我们只有一个 appsettings.Release.json 文件也根据正在部署的环境获取替换值.

OK so I have it working now :) Because we use octopus deploy we don't want multiple config files so we just have the one appsettings.Release.json file which gets the values substituted base on the environment being deployed too.

以下是主要功能代码.

public static class Function
    {
        // Format in a CRON Expression e.g. {second} {minute} {hour} {day} {month} {day-of-week}
        // https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer
        // [TimerTrigger("0 59 23 * * *") = 11:59pm
        [FunctionName("Function")]
        public static void Run([TimerTrigger("0 59 23 * * *")]TimerInfo myTimer, ILogger log)
        {

            // If running in debug then we dont want to load the appsettings.json file, this has its variables substituted in octopus
            // Running locally will use the local.settings.json file instead
#if DEBUG
            IConfiguration config = new ConfigurationBuilder()
                .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();
#else
            IConfiguration config = Utils.GetSettingsFromReleaseFile();
#endif

            // Initialise dependency injections
            var serviceProvider = Bootstrap.ConfigureServices(log4Net, config);

            var retryCount = Convert.ToInt32(config["RetryCount"]);

            int count = 0;
            while (count < retryCount)
            {
                count++;
                try
                {
                    var business = serviceProvider.GetService<IBusiness>();
                    business.UpdateStatusAndLiability();
                    return;
                }
                catch (Exception e)
                {
                    // Log your error
                }
            }

        }

    }

Utils.cs 文件如下所示

public static class Utils
    {

        public static string LoadSettingsFromFile(string environmentName)
        {
            var executableLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            // We need to go back up one level as the appseetings.Release.json file is not put in the bin directory
            var actualPathToConfig = Path.Combine(executableLocation, $"..\appsettings.{environmentName}.json");
            using (StreamReader reader = new StreamReader(actualPathToConfig))
            {
                return reader.ReadToEnd();
            }
        }

        public static IConfiguration GetSettingsFromReleaseFile()
        {
            var json = Utils.LoadSettingsFromFile("Release");
            var memoryFileProvider = new InMemoryFileProvider(json);

            var config = new ConfigurationBuilder()
                .AddJsonFile(memoryFileProvider, "appsettings.json", false, false)
                .Build();
            return config;
        }

    }

appsettings.Release.json 在 Visual Studio 中设置为 ContentCopy Always.看起来像这样

The appsettings.Release.json is set as Content and Copy Always in visual studio. It looks like this

{
  "RetryCount": "#{WagonStatusAndLiabilityRetryCount}",
  "RetryWaitInSeconds": "#{WagonStatusAndLiabilityRetryWaitInSeconds}",
  "DefaultConnection": "#{YourConnectionString}"
}

实际上我相信你可以在那里有一个 appsettings.config 文件并跳过 appsettings.Release.json 文件,但这是有效的,你现在可以用它做你想做的事.

Actually I believe you could have an appsettings.config file there already and skip the appsettings.Release.json file, but this is working and you can do what you want with it now.

这篇关于Azure Functions,如何拥有多个 .json 配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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