Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 599
Josh Avatar asked Mar 22 '18 05:03

Josh


People also ask

How do I subscribe to Azure Service Bus?

In the left navigation pane of the portal, select + Create a resource, select Integration, and then select Service Bus. In the Basics tag of the Create namespace page, follow these steps: For Subscription, choose an Azure subscription in which to create the namespace.

How do I create a Service Bus in Azure portal?

Create a queue in the Azure portalOn the Service Bus Namespace page, select Queues in the left navigational menu. On the Queues page, select + Queue on the toolbar. Enter a name for the queue, and leave the other values with their defaults. Now, select Create.


3 Answers

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]));
        }
    }
}
like image 52
Gustavo Armenta Avatar answered Oct 14 '22 07:10

Gustavo Armenta


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.

like image 31
Sean Feldman Avatar answered Oct 14 '22 07:10

Sean Feldman


Microsoft.Azure.ServiceBus has been deprecated. The new option is Azure.Messaging.ServiceBus and ManagementClient has been replaced by ServiceBusAdministrationClient.

string connectionString = "<connection_string>";
ServiceBusAdministrationClient client = new ServiceBusAdministrationClient(connectionString);

This new package also supports ManagedIdentity:

string fullyQualifiedNamespace = "yournamespace.servicebus.windows.net";
ServiceBusAdministrationClient client = new ServiceBusAdministrationClient(fullyQualifiedNamespace, new DefaultAzureCredential());

A little example:

var queueExists = await _administrationClient.QueueExistsAsync(queueName);
        if(!queueExists)
            await _administrationClient.CreateQueueAsync(queueName);

More info here.

like image 3
Augusto Diaz Avatar answered Oct 14 '22 06:10

Augusto Diaz