是否可以将AzureAppConfiguration与Azure函数ServiceBusTrigger一起使用

是否可以将AzureAppConfiguration与Azure函数ServiceBusTrigger一起使用,azure,azure-functions,azureservicebus,azure-app-configuration,Azure,Azure Functions,Azureservicebus,Azure App Configuration,今天,我有了一个带有ServiceBustigger的Azure函数,可以从我的设置文件中读取值。像这样: [FunctionName("BookingEventListner")] public static async Task Run([ServiceBusTrigger("%topic_name%", "%subscription_name%", Connection = "BookingservicesTopicEndpoint")]Microsoft.Azure.ServiceBus

今天,我有了一个带有ServiceBustigger的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);
    });
});
致意 马格纳斯

你可以用它来实现它

首先,使用获取端点、主题和订阅名

var builder = new ConfigurationBuilder();
builder.AddAzureAppConfiguration(Environment.GetEnvironmentVariable("ConnectionString"));
var config = builder.Build();
string message = config["TestApp:Settings:Message"];
然后使用
servicebustriggeratAttribute
获取主题名称和属性订阅

var attributes = new Attribute[]
{
    new ServiceBusAccountAttribute("yourservicebusname"),
    new ServiceBusTriggerAttribute(topic,sub)
};
var outputSbMessage = await binder.BindAsync<IAsyncCollector<BrokeredMessage>>(attributes);
var attributes=新属性[]
{
新的ServiceBusAccountAttribute(“您的ServiceBusName”),
新ServiceBusTriggerAttribute(主题,子主题)
};
var outputSbMessage=await binder.BindAsync(属性);

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

这在路上帮助了我,我就是这样做的。首先,我为IWebJobsBuilder接口创建一个扩展方法

   /// <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;
    }
在我的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配置获取信息。我想从函数中提取消息的位置获取信息。或者我错过了什么,所以你没有从我这里得到信息?并从local.setting.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}");
}