22
votes

My scenario: Website hosted on the cloud, where each instance creates a subscription to a Service Bus Topic for itself to listen for messages.

My question: How do I programmatically create subscriptions?

2
I hear you. Have been asking for management operations since day one. Good news is that the team is listening. Perhaps to make it more realistic, it will take time. Suggest to voice your opinion in the GitHub issue I've linked in my answer. - Sean Feldman
Josh, you could consider marking Gustavo's answer as the accepted answer, as it will help others that land on this question. - Raj Rao

2 Answers

44
votes

Microsoft.Azure.ServiceBus.3.1.0 allows to create a ManagementClient using the ConnectionString.

private async Task CreateTopicSubscriptions()
{
    var client = new ManagementClient(ServiceBusConnectionString);
    for (int i = 0; i < Subscriptions.Length; i++)
    {
        if (!await client.SubscriptionExistsAsync(TopicName, Subscriptions[i]))
        {
            await client.CreateSubscriptionAsync(new SubscriptionDescription(TopicName, Subscriptions[i]));
        }
    }
}
6
votes

Original plan for the new Azure Service Bus client was not to include management plane at all and use Azure Active Directory route instead. This has proven to be too problematic, just like you've pointed out. Microsoft messaging team has put together a sample to demonstrate the basic operations.

Note that there's a pending PR to get it working with .NET Core 2.0

Moving forward, it was recognized that developers prefer to access Service Bass using a connection string like they used to over Azure Active Directory option. Management Operations issue is raised to track requests. Current plan is to provide a light weight management library for the .NET Standard client.

For now, the options are either to leverage the old client to create entities or use Microsoft.Azure.Management.ServiceBus (or Fluent) until the management package is available.

Update

Management operations were released as part of 3.1.0 version of the client.