Search code examples
c#azureservicebus.net-standard

Programmatically create service bus subscription using .net standard


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?


Solution

  • 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]));
            }
        }
    }