Azure Functions,如何拥有多个 .json 配置文件

And*_*rew 6 c# azure azure-functions asp.net-core-2.0

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

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

And*_*rew 3

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

下面是主要功能代码。

public static class Function
    {
        // Format in a CRON Expression e.g. {second} {minute} {hour} {day} {month} {day-of-week}
        // https://learn.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
                }
            }

        }

    }
Run Code Online (Sandbox Code Playgroud)

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

    }
Run Code Online (Sandbox Code Playgroud)

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

{
  "RetryCount": "#{WagonStatusAndLiabilityRetryCount}",
  "RetryWaitInSeconds": "#{WagonStatusAndLiabilityRetryWaitInSeconds}",
  "DefaultConnection": "#{YourConnectionString}"
}
Run Code Online (Sandbox Code Playgroud)

实际上,我相信您已经有一个 appsettings.config 文件并跳过 appsettings.Release.json 文件,但这正在工作,您现在可以用它做您想做的事情。