Paul
Paul

Reputation: 2047

How can I get the service bus topic from configuration when using IAsyncCollector?

I have an Azure Function that publishes some messages to the service bus, declared like this:

[FunctionName(FunctionNames.PublishMessages)]
public async Task PublishMessages(
    [ActivityTrigger] IEnumerable<Foo> messages,
    [ServiceBus("TopicName", Connection = "ServiceBusConnectionString")] IAsyncCollector<string> collector,
    ILogger log)
{
    foreach (var message in messages)
    {
        await collector.AddAsync(JsonSerializer.Serialize(message));
    }

    await collector.FlushAsync();
}

The output binding for the service bus takes the name of the topic and the name of the configuration key whose value contains the connection string.

Instead of hard-coding the topic in the method signature I'd like to get it from the function's configuration, in the same way I get the connection string. Is this possible? Any help much appreciated.

Upvotes: 1

Views: 286

Answers (1)

silent
silent

Reputation: 16148

You can do that by referencing it using %. So

[ServiceBus("%TopicNameSetting%", Connection = ...

and create an app setting with the key TopicNameSetting

Upvotes: 1

Related Questions