Refer Topic and Subscription name from Configuration in an Azure Function with Service Bus Trigger
Asked Answered
M

1

11

I have an Azure Service Bus with Service Bus Topic trigger. My function looks something like this

[FunctionName("SbListener")]
        public static async Task Run(
            [ServiceBusTrigger("test-topic", "test-sub-1", Connection = "ServiceBus")]string message, 
            [Inject("Microsoft.EventStore.Functions", true)] IWebNotificationManagerFactory webNotificationManagerFactory,
            [Inject("Microsoft.EventStore.Functions", true)] ILogger logger)
        { ... }

The configuration for my Service Bus is in the local.settings.json file.

"ConnectionStrings": {
    "ServiceBus": "Endpoint=sb://<my-sb>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<my-key>"
}

What I am looking for is that I want to refer the topic names from the configuration file as well, rather than hard-coding them in the ServiceBusTrigger. The problem is that in case I change the subscription name then I would have to re-deploy Function code (I want to avoid this at all cost).

Mala answered 22/2, 2019 at 13:40 Comment(0)
S
30

Put topic and subscription in Values in local.settings.json(Application settings in portal) and reference them using app setting binding expressions--wrap the app setting name with %, check the doc.

[ServiceBusTrigger("%Topic%", "%Subscription%", Connection = "ServiceBus")]string message

Besides, I would suggest you put ServiceBus connection string in Values as well, ConnectionStrings is used by frameworks that typically get connection strings from the ConnectionStrings section of a configuration file, such as Entity Framework. See the doc.

Spokeswoman answered 22/2, 2019 at 13:53 Comment(1)
Perfect! Not sure how I missed it.Mala

© 2022 - 2024 — McMap. All rights reserved.