azure - 是否可以将 AzureAppConfiguration 与 Azure Function ServiceBusTrigger 一起使用

标签 azure azure-functions azureservicebus azure-app-configuration

今天,我有一个带有 ServiceBusTrigger 的 Azure 函数,可以从我的设置文件中读取值。像这样:

[FunctionName("BookingEventListner")]
public static async Task Run([ServiceBusTrigger("%topic_name%", "%subscription_name%", Connection = "BookingservicesTopicEndpoint")]Microsoft.Azure.ServiceBus.Message mySbMsg, ILogger log)
{

但是我在此解决方案中将 Azure 应用程序配置与其他项目一起使用,并且希望将端点、主题和下标名称也存储到 Azure 应用程序配置中(添加它们不是问题,但检索它们是问题)。

是否可以通过某种方式将 AzureAppConfiguration 提供程序添加到配置处理程序中,就像我可以在 Web 应用程序中执行的那样?

webHostBuilder.ConfigureAppConfiguration((context, config) =>
{
    var configuration = config.Build();
    config.AddAzureAppConfiguration(options =>
    {
        var azureConnectionString = configuration[TRS.Shared.AspNetCore.Constants.CONFIGURATION_KEY_AZURECONFIGURATION_CONNECTIONSTRING];

        if (string.IsNullOrWhiteSpace(azureConnectionString)
                || !azureConnectionString.StartsWith("Endpoint=https://"))
            throw new InvalidOperationException($"Missing/wrong configuration value for key '{Constants.CONFIGURATION_KEY_AZURECONFIGURATION_CONNECTIONSTRING}'.");

        options.Connect(azureConnectionString);
    });
});

致以诚挚的问候 马格努斯

最佳答案

我在这里找到了一个有用的链接:http://marcelegger.net/azure-functions-v2-keyvault-and-iconfiguration#more-45

这对我很有帮助,我就是这么做的。首先,我为 IWebJobsBuilder 接口(interface)创建一个扩展方法。

   /// <summary>
    /// Set up a connection to AzureAppConfiguration
    /// </summary>
    /// <param name="webHostBuilder"></param>
    /// <param name="azureAppConfigurationConnectionString"></param>
    /// <returns></returns>
    public static IWebJobsBuilder AddAzureConfiguration(this IWebJobsBuilder webJobsBuilder)
    {
        //-- Get current configuration
        var configBuilder = new ConfigurationBuilder();
        var descriptor = webJobsBuilder.Services.FirstOrDefault(d => d.ServiceType == typeof(IConfiguration));
        if (descriptor?.ImplementationInstance is IConfigurationRoot configuration)
            configBuilder.AddConfiguration(configuration);

        var config = configBuilder.Build();

        //-- Add Azure Configuration
        configBuilder.AddAzureAppConfiguration(options =>
        {
            var azureConnectionString = config[TRS.Shared.Constants.CONFIGURATION.KEY_AZURECONFIGURATION_CONNECTIONSTRING];

            if (string.IsNullOrWhiteSpace(azureConnectionString)
                    || !azureConnectionString.StartsWith("Endpoint=https://"))
                throw new InvalidOperationException($"Missing/wrong configuration value for key '{TRS.Shared.Constants.CONFIGURATION.KEY_AZURECONFIGURATION_CONNECTIONSTRING}'.");

            options.Connect(azureConnectionString);
        });
        //build the config again so it has the key vault provider
        config = configBuilder.Build();

        //replace the existing config with the new one
        webJobsBuilder.Services.Replace(ServiceDescriptor.Singleton(typeof(IConfiguration), config));
        return webJobsBuilder;
    }

azureConnectionString 是从 appsetting.json 读取的,并且应包含 Azure 应用程序配置的 URL。

完成后,我们需要在 Azure Func 项目中创建一个“启动”类,如下所示。

   public class Startup : IWebJobsStartup
    {
        //-- Constructor
        public Startup() { }

        //-- Methods
        public void Configure(IWebJobsBuilder builder)
        {
            //-- Adds a reference to our Azure App Configuration so we can store our variables there instead of in the local settings file.
            builder.AddAzureConfiguration(); 
            ConfigureServices(builder.Services)
                .BuildServiceProvider(true);
        }
        private IServiceCollection ConfigureServices(IServiceCollection services)
        {
            services.AddLogging();
            return services;
        }
    }

在我的 func 类中,我现在可以从 Azure 应用程序配置中提取值,就像它们在 appsetting.json 文件中写入一样。

[FunctionName("FUNCTION_NAME")]
public async Task Run([ServiceBusTrigger("%KEYNAME_FOR_TOPIC%", "%KEYNAME_FOR_SUBSCRIPTION%", Connection = "KEY_NAME_FOR_SERVICEBUS_ENDPOINT")]Microsoft.Azure.ServiceBus.Message mySbMsg
    , ILogger log)
{
    log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg.MessageId}");
}

关于azure - 是否可以将 AzureAppConfiguration 与 Azure Function ServiceBusTrigger 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56951957/

相关文章:

azure - Azure服务总线队列如何保证最多一次交付?

c# - Azure Functions - 输出绑定(bind)理由

azure - 我在 azure 存储资源管理器中没有 'azure-webjobs-hosts' 容器,因此我的 azure 函数应用程序将无法运行

azure - .Net Core Azure服务总线获取当前消息计数

Azure 服务总线订阅者死信

java - java中的超时值超过启动取消azure函数

c# - 无法在Azure服务器上安装VHD驱动器

sql-server - SQLx 到 Mssql 连接字符串

azure - 创建新应用程序

c# - 如果我每隔一小时运行 azure WebJob,我会收到印度标准时区延迟 30 分钟的通知