是否可以将AzureAppConfiguration与Azure Function ServiceBusTrigger一起使用 [英] Is it possible to use AzureAppConfiguration together with an Azure Function ServiceBusTrigger

查看:78
本文介绍了是否可以将AzureAppConfiguration与Azure Function ServiceBusTrigger一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Today I have an Azure Function with the ServiceBusTrigger that reads values from my settings file. Like this:

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

但是在此解决方案中,我正在将Azure App Configuration与其他项目一起使用,并且还希望将终结点,主题和下标名称也存储到Azure App Configuration中(很好地添加它们不是问题,但检索它们则是问题).

But I am using Azure App Configuration with other projects in this solution and would like to store the endpoint, topic and subscriptname into the Azure App Configuration also (well adding them is not a problem but retrieving them are).

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

Is there someway to add the AzureAppConfiguration provider to the configuration handler, just that I can do in a web-app?

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

最好的问候马格努斯

推荐答案

我在这里找到了有用的链接:

I found a helpful link here: http://marcelegger.net/azure-functions-v2-keyvault-and-iconfiguration#more-45

这对我有帮助,这就是我的方法.首先,我为IWebJobsBuilder接口创建了一个扩展方法.

That helped me on the way, and this is how I do it. First I create an extensions method for the 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;
    }

从appsetting.json中读取azureConnectionString的位置,其中应包含Azure应用程序配置的URL.

Where the azureConnectionString is read from you appsetting.json and should contain the url to the Azure App Configuration.

完成此操作后,我们需要在Azure Func项目中创建一个启动"类,看起来像这样.

When that is done we need to create a "startup" class in the Azure Func project, that will look like this.

   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文件中写入的一样.

in my func class I can now extract the values from my Azure App Configuration exactly as if they where written in my appsetting.json file.

[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}");
}

这篇关于是否可以将AzureAppConfiguration与Azure Function ServiceBusTrigger一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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