Andrew Williamson
Andrew Williamson

Reputation: 8691

Referencing environment variable for topic name in ServiceBusTrigger

I'd like to have items added to a service bus topic, then pulled off the 'Live' subscription and sent to the live site, and pulled off the 'Development' subscription and sent to the dev site.

[FunctionName("AddFoo")]
public static async Task AddFooAsync(
    [ServiceBusTrigger("topic-foo", "Live")]QueueItem item,
    TraceWriter log)
{
    var endpoint = ConfigurationManager.AppSettings["EndPoint"];
    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Add("PublisherKey", foo.PublisherKey);
    var foos = new HttpFooStore(httpClient, endpoint);

    try
    {
        await foos.AddAsync(item.Value);
    }
    catch (BadRequestException)
    {
        log.Warning("Malformed request was rejected by XXX", item.PublisherName);
        return;
    }
    catch (AuthorizationException)
    {
        log.Warning("Unauthorized request was rejected by XXX", item.PublisherName);
        return;
    }
    catch (ResourceNotFoundException)
    {
        log.Warning("Request for unknown tracker was rejected by XXX", item.PublisherName);
        return;
    }
    catch (Exception e)
    {
        log.Error("Request to XXX was unsuccessful", e, item.PublisherName);
        throw e;
    }
}

The implementation of the function is exactly the same, the only thing that is different is the name of the subscription, and the endpoint used. Unfortunately, the subscription name is part of an annotation, so it has to be a constant. Is there any way I can get the desired effect without having to duplicate all the code?

Edit

To clarify, I want to create two separate deployments - one for live, and one for development. For each deployment, I would update the environment settings, which would determine which subscription the function is bound to.

Upvotes: 3

Views: 3119

Answers (2)

Roman Kiss
Roman Kiss

Reputation: 8265

You can refer to environment variables by surrounding them in percentage signs:

ServiceBusTrigger("%myTopic%", "%mySubscription%")

Where myTopic and mySubscription are environment variables in the app settings.

Upvotes: 11

Marie Hoeger
Marie Hoeger

Reputation: 1331

You can't have a single function triggered by two service bus topics (development vs. live), but you can move the meat of your function into a helper method that can be called from both functions.

Upvotes: 0

Related Questions