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

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

问题描述

所以我写了一个可以在本地工作的Azure函数.我认为这取决于拥有local.setting.json文件.但是,当我将其发布为天蓝色时,该功能将无法正常工作,因为它无法找到我定义的设置值.来自Web应用程序和控制台驱动的方法,我们将拥有与每个环境相关联的不同配置文件.如何使它工作,这样我可以有多个settings.json文件,例如一个适合开发人员,雄鹿和产品环境?最终结果是使用octopus deploy进行部署,但是,如果此时我无法通过publish进行部署,则没有机会这样做.

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 event 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?

推荐答案

好的,现在我可以使用它了:)因为我们使用章鱼部署,所以我们不需要多个配置文件,所以只有一个 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.

下面是主要功能代码.

Below is the main function code.

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中设置为内容始终复制.看起来像这样

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函数,如何具有多个.json配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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