Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different service bus connection strings for ServiceBusTrigger WebJob

I have a WebJob which reads messages from an event topic, processes them and then creates a message on a different topic.

I can achieve this easily using a service bus trigger.

public void EventSubscriptionToNotificationTopic(
        [ServiceBusTrigger(Subscribe.TopicName, Subscribe.SubscriptionName)] BrokeredMessage input,
        [ServiceBus(Publish.TopicName)] out BrokeredMessage output)

To do this we have to use a service bus connection string which contains a shared access key which permits send and listen permissions at a level which has access to both topics (root).

We would like to be able to use different connection strings/SAS tokens locked down to permissions we need on these topics (Listen on event topic subscription and Send for the topic to publish to).

Is at all possible to specify which connection a service bus trigger or attribute would use?

If not would I have to roll my own, maybe just using the service bus trigger and WebJob connection string to read the message and then use a TopicClient to create a new message on the publish topic?

like image 667
Robert Roe Avatar asked Mar 11 '23 03:03

Robert Roe


1 Answers

There is a ServiceBusAccountAttribute that let you specify the connection string you want to use. you'll also need to specify the AccessRights:

  • Different connection string for output or trigger
  • WebJob ServiceBus Topic permissions

so your code can look this:

public void EventSubscriptionToNotificationTopic(
    [ServiceBusTrigger(Subscribe.TopicName, Subscribe.SubscriptionName, AccessRights.Listen),
     ServiceBusAccount("Topic1Listen")] BrokeredMessage input,
    [ServiceBus(Publish.TopicName, AccessRights.Send),
     ServiceBusAccount("Topic2Send")] out BrokeredMessage output)
    {
        ...
    }

Topic1Listen and Topic2Send are the names of the connection in your app.config but in the config file you need to prefix the name of the connectionstring with AzureWebJobs

So in your config file you'll need two connectionstrings that looks like that:

<connectionStrings>
    ...
    <add name="AzureWebJobsTopic1Listen" connectionString="..." />
    <add name="AzureWebJobsTopic2Send" connectionString="..." />
</connectionStrings>
like image 80
Thomas Avatar answered Apr 08 '23 14:04

Thomas