2

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.

2
  • ServiceBusTrigger("%myTopic%", "%mySubscription%"), where myTopic and mySubscription are environment variables in the appsettings
    – Roman Kiss
    Apr 17, 2018 at 0:47
  • @RomanKiss this is exactly what I was looking for, thanks! I'm just testing it out now, do you want to write that up as an answer? Apr 17, 2018 at 0:52

2 Answers 2

8

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.

1
  • Worked great as of 7/2023.
    – Jeff B
    Jul 6, 2023 at 16:04
0

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.

1
  • I'll update the question to clarify - I want to deploy this function once for each environment, rather than having a single function with two triggers in the same environment Apr 16, 2018 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.