简体   繁体   中英

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? *

Below is the updated working code, you need to make sure you have shared access policy with full access.

共享访问策略

using Azure.Messaging.ServiceBus.Administration;
using System;
using System.Threading.Tasks;

namespace ServiceBusDemo
{
    class Program
    {
        private static string connectionString = "Endpoint=sb://ns-servicebusshu.servicebus.windows.net/;SharedAccessKeyName=fullAccess;SharedAccessKey=oB+IsK8Aqp0/xfXnF9HCz6x9pqPIOysTXaJofSmHEYs=";
        private static string queueName = "testqueue";

        async static Task Main(string[] args)
        {
            await doit();
        }
       
        public static async Task doit()
        {
            var adminClient = new ServiceBusAdministrationClient(connectionString);
            bool queueExists = await adminClient.QueueExistsAsync(queueName);

            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 = await adminClient.CreateQueueAsync(options);
            }

        }
    }
}

Once you ran the application its successfully created the queue as below:

在此处输入图像描述

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.

Maybe it's not your case... But if you have a TOPIC with the same name that you try to create your new QUEUE, QueueExistsAsync will return false , but you'll be spitted with this bizarre error at creation time. The fix is easy... changing the queue name or deleting the offending topic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM