Search code examples
c#azureservicebus

Cannot create a new Topic in ServiceBus with CreateTopicAsync


I'm trying to create a new Topic (and then a subscription) with the method CreateTopicAsync of the library Microsoft.Azure.ServiceBus.Management. The connection string is correct and I can send and receive messages if I create the topic trough the Azure portal. What am I doing wrong? Any help is appreciated.

var managementClient = new ManagementClient(ServiceBusConnectionString);
bool topicExists = await managementClient.TopicExistsAsync(TopicName).ConfigureAwait(false);
if (!topicExists) {
    TopicDescription td = new TopicDescription(TopicName);
    td.MaxSizeInMB = 1024;
    td.DefaultMessageTimeToLive = new TimeSpan(2, 0, 0, 0);
    await managementClient.CreateTopicAsync(td).ConfigureAwait(false);
}

Solution

  • Although the Service Bus ConnectionString might be correct, for your application to be able to create a Topic it needs to have the Manage right (claim).

    Taken from Service Bus access control with Shared Access Signatures:

    For each authorization policy rule, you decide on three pieces of information: name, scope, and rights. The name is just that; a unique name within that scope. The scope is easy enough: it's the URI of the resource in question. For a Service Bus namespace, the scope is the fully qualified domain name (FQDN), such as https://.servicebus.windows.net/.

    The rights conferred by the policy rule can be a combination of:

    • 'Send' - Confers the right to send messages to the entity
    • 'Listen' - Confers the right to listen (relay) or receive (queue, subscriptions) and all related message handling
    • 'Manage' - Confers the right to manage the topology of the namespace, including creating and deleting entities

    The 'Manage' right includes the 'Send' and 'Receive' rights.