Create ServiceBus topic if it doesn't already exist

service bus create topic programmatically
azure service bus .net core
service bus topic send message
azure service bus tutorial
azure service bus connection string
getting started with azure service bus topics
azure service bus explorer
azure service bus local development

Microsoft has updated their .NET ServiceBus client library, and their documentation is currently split between the old WindowsAzure.ServiceBus package and the new Microsoft.Azure.ServiceBus package. I like the new package, as its a lot cleaner and has less dependencies. In the old package, we had methods like the following:

if (!namespaceManager.TopicExists(topicName))
{
    var topic = new TopicDescription(topicName);
    namespaceManager.CreateTopic(topic);
}

The documentation for creating a topic programmatically still uses the old package, with code like the above. The NamespaceManager class is not available in the new package, so how can I achieve the equivalent of this?

Azure Service Bus frequently asked questions (FAQ), Creating a namespace is necessary to use Service Bus and is one of the first steps in What are Azure Service Bus topics and subscriptions? Follow steps in the Quickstart: Use the Azure portal to create a Service Bus topic and subscriptions to the topic to do the following tasks: Create a Service Bus namespace. Get the connection string. Create a topic in the namespace. Create one subscription to the topic in the namespace. Visual Studio 2017 Update 3 (version 15.3, 26730.01) or later.

In .NET Core you can use ManagementClient to do so, which is easier comparing to namespace manager.

try
{   
    await managementClient.GetTopicAsync(topicName);
}
catch (MessagingEntityNotFoundException)
{   
    await managementClient.CreateTopicAsync(new TopicDescription(topicName) { EnablePartitioning = true });
}

try
{
    await managementClient.GetQueueAsync(queueName);
}
catch (MessagingEntityNotFoundException)
{
    await managementClient.CreateQueueAsync(new QueueDescription(queueName) { EnablePartitioning = true });
}

See azure-service-bus-dotnet/issues/65

Use the Azure portal to create Service Bus topics and subscriptions , Quickstart: In this quickstart, you learn how to create a Service Bus topic and subscriptions to that topic by using the Azure portal. Service Bus topics. You can route events in Event Grid directly to Service Bus topics to handle Azure system events with Service Bus topics, or for command & control messaging scenarios. In the Azure portal, while creating an event subscription, select Service Bus Topic as endpoint type and then click select and endpoint to choose a Service Bus

Messaging with Azure Service Bus - Part 7, The difference is that a topic doesn't store messages. az servicebus topic subscription create -g $resourceGroup ` --namespace-name  az servicebus topic authorization-rule Manage Azure Service Bus Topic Authorization Rule. az servicebus topic authorization-rule create Create Authorization Rule for given Service Bus Topic. az servicebus topic authorization-rule delete Deletes the Authorization Rule of the given Service Bus Topic

Azure ServiceBus in .NET Core: Managing topics, queues and , The new package doesn't include any API to manage these resources. The only To do that, I have created a service principal and given it a  Service Bus topics and subscriptions enable you to scale to process a large number of messages across a large number of users and applications. Create a namespace in the Azure portal To begin using Service Bus messaging entities in Azure, you must first create a namespace with a name that is unique across Azure.

Management Operations · Issue #65 · Azure/azure-service-bus , Seems that old windows version of Azure Service Bus is closed source (or maybe I could not find its source) so I The management library doesn't connect using connection string. via the resource manager to create service bus accounts but not to create queues inside of a service bus. Sorry that this is slightly off-topic. If you don't have a topic and subscription to work with, follow steps in the Use Azure portal to create a Service Bus topics and subscriptions article to create them. Note down the connection string for your Service Bus instance and the names of the topic and subscription you created. We'll use these values in the samples.

Implementing a Retry Pattern for Azure Service Bus with Topic Filters, A problem arises when you try to resubmit a DLQ message. Azure Service Bus by design doesn't support message resubmission. This means that you cannot  So I've recently gotten the need to use Service Bus Topic and Subscriptions and I've followed many articles and tutorials. I've been able to successfully implement Microsoft's Get started with Service Bus topics and also successfully used Visual Studio 2017's Worker Role template to access a database.

Comments
  • Thanks Thomas. I skimmed over the readme and didn't pick up that "manage service bus entities" equates to "create/read/update topics and subscriptions". What's the deal with the "resource group name"? I assume this is the same as resource groups I see in the Azure portal, but doesn't that make code like this a bit easier to break if the resources are shifted around in Azure?
  • Yes it is the name of the resource group on Azure. I guess it depends if you are using multiple resource groups. Maybe you can ask another question to deal with it.