Search code examples
c#.netazureservicebus

Error when creating ServiceBus Queue using Azure.Messaging.ServiceBus.Administration


I am (trying) to use this code to create ServiceBus Queue:

using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
...
class blabla
{
   private string connectionString = "Endpoint=sb://XXXX.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=XXXYYY";
   private string queueName = "testqueue";
   ...
   public doit()
   {
    var adminClient = new ServiceBusAdministrationClient(connectionString);
    bool queueExists = adminClient.QueueExistsAsync(queueName).Result;
    if (!queueExists)
    {
        var options = new CreateQueueOptions(queueName)
        {
            DefaultMessageTimeToLive = TimeSpan.FromDays(2),
            LockDuration = TimeSpan.FromSeconds(45),
            MaxDeliveryCount = 8,
            MaxSizeInMegabytes = 2048
        };
        options.AuthorizationRules.Add(new SharedAccessAuthorizationRule(
            "allClaims",
            new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen }));
        QueueProperties createdQueue = adminClient.CreateQueueAsync(options).Result;
    }

   }
}

but constantly getting this error:

System.AggregateException: One or more errors occurred. (SubCode=40900. Conflict. You're requesting an operation that isn't allowed in the resource's current state. To know more visit https://aka.ms/sbResourceMgrExceptions. . TrackingId:bc79fd98-73c8-4301-b6b9-05d0eae6ed6a_G17, SystemTracker:xxx.servicebus.windows.net:yyy, Timestamp:2021-05-09T00:24:57
Status: 409 (Conflict)
ErrorCode: 40900

Using old (NET) way with NamespaceManager from Microsoft.ServiceBus works with no problems.

  var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
  if (!namespaceManager.QueueExists(queueName))
  {
     namespaceManager.CreateQueue(queueName);
  }

So, does anyone knows what am I doing wrong here? *


Solution

  • Sorry for the confusion. My code (and Rahul Shukla as well) is working now. I had to create a few new shared access policies with full access. The third created started working. The previous 2 I created are still not working. There are no differences between the 3 policies created. Hence the question marks in my answer. Posted question on MS NET SB forum about 1 out of 3 policies working. No answer/acknowledgment so far.