You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为带ServiceBusTrigger的Azure函数集成Azure App Configuration?

当然可以!Azure Functions完全支持集成Azure App Configuration

实现思路和你熟悉的Web应用非常相似,只是在启动配置的入口上略有不同。下面我一步步给你拆解具体实现步骤:

1. 安装必要的NuGet包

首先需要安装Azure App Configuration针对Functions的专用包,在你的Azure Functions项目中安装Microsoft.Azure.AppConfiguration.Functions(如果是.NET 6+的隔离进程模型,可搭配Microsoft.Extensions.Configuration.AzureAppConfiguration使用)。

你可以通过NuGet包管理器控制台执行:

Install-Package Microsoft.Azure.AppConfiguration.Functions

或者在.NET CLI中执行:

dotnet add package Microsoft.Azure.AppConfiguration.Functions

2. 添加Startup配置类

在你的Functions项目中创建一个Startup类,继承自FunctionsStartup,并通过[assembly: FunctionsStartup]标记指定启动入口。在这里你就可以像Web应用那样添加Azure App Configuration作为配置源:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using System;

[assembly: FunctionsStartup(typeof(YourNamespace.Startup))]
namespace YourNamespace
{
    public class Startup : FunctionsStartup
    {
        public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
        {
            var context = builder.GetContext();
            
            // 先加载本地配置(比如local.settings.json)获取Azure App Configuration的连接字符串
            var config = builder.ConfigurationBuilder.Build();
            var appConfigConnString = config["CONFIGURATION_KEY_AZURECONFIGURATION_CONNECTIONSTRING"];
            
            if (string.IsNullOrWhiteSpace(appConfigConnString) || !appConfigConnString.StartsWith("Endpoint=https://"))
            {
                throw new InvalidOperationException($"Missing/wrong configuration value for key 'CONFIGURATION_KEY_AZURECONFIGURATION_CONNECTIONSTRING'.");
            }
            
            // 添加Azure App Configuration作为配置源
            builder.ConfigurationBuilder.AddAzureAppConfiguration(options =>
            {
                options.Connect(appConfigConnString);
                // 可选:如果你的配置项有统一前缀,可以添加筛选器缩小范围,比如
                // options.Select("ServiceBus:*");
                // 可选:配置自动刷新策略,无需重启函数即可获取最新配置
                // options.ConfigureRefresh(refresh => 
                //     refresh.Register("topic_name", refreshAll: true)
                //            .SetCacheExpiration(TimeSpan.FromMinutes(5)));
            });
        }
    }
}

3. 验证配置读取

完成上述配置后,你原来的Function代码不需要做太多修改,触发器中用%key%格式引用的配置项会自动从Azure App Configuration中读取。

比如你在Azure App Configuration中添加以下配置项:

  • topic_name: 你的Service Bus主题名称
  • subscription_name: 你的Service Bus订阅名称
  • BookingservicesTopicEndpoint: Service Bus的连接字符串(也可以存储在App Configuration中)

你的Function代码依然可以保持原来的写法:

[FunctionName("BookingEventListner")]
public static async Task Run(
    [ServiceBusTrigger("%topic_name%", "%subscription_name%", Connection = "BookingservicesTopicEndpoint")] Microsoft.Azure.ServiceBus.Message mySbMsg, 
    ILogger log)
{
    // 你的业务逻辑代码...
}

几个注意事项

  • 本地开发:在local.settings.json中需要保留CONFIGURATION_KEY_AZURECONFIGURATION_CONNECTIONSTRING的配置值,这样本地调试时才能连接到Azure App Configuration。
  • 生产部署:部署到Azure后,建议在Function App的应用设置中添加CONFIGURATION_KEY_AZURECONFIGURATION_CONNECTIONSTRING;更安全的方式是使用托管标识访问Azure App Configuration,此时可以把options.Connect(appConfigConnString)替换为options.Connect(new Uri(appConfigEndpoint), new DefaultAzureCredential()),避免暴露连接字符串。
  • 配置刷新:如果需要实现在不重启函数的情况下更新配置,可以启用自动刷新策略(上面代码中的可选部分),并在函数中注入IConfigurationRefresherProvider来触发刷新逻辑。

内容的提问来源于stack exchange,提问作者Magnus Gladh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:06:15