如何使用 C# 创建 Microsoft Azure 服务总线队列?

How to create queue of microsoft azure service bus using c#?

如何使用 C# 代码创建 Microsoft Azure 服务总线队列?

我已经试过了,但是没有用:

string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists("testqueue")) {
   namespaceManager.CreateQueue("testqueue");
}

试试下面的代码,

public static void CreateQueue(NamespaceManager nameSpaceManager, string queueName)
        {
            if (!nameSpaceManager.QueueExists(queueName))
            {
                var qd = new QueueDescription(queueName)
                {
                    MaxSizeInMegabytes = 5120,
                    DefaultMessageTimeToLive = new TimeSpan(0, 1, 0)
                    //IsAnonymousAccessible = true
                };

                nameSpaceManager.CreateQueue(qd);
            }
        }

并调用它

var nameSpaceManager = NamespaceManager.CreateFromConnectionString(serviceBusConnectionString);
                CreateQueue(nameSpaceManager, queueName);

NamespaceManager 来自旧的且已弃用的 Azure 服务总线 .NET 库 WindowsAzure.ServiceBus that shouldn't be used for new development. Instead, the new .NET Standard client Microsoft.Azure.ServiceBus 应该使用。新客户端提供类似的功能,但结构略有不同。静态 NamespaceManager class 替换为 ManagementClient class。其余部分在逻辑上是相同的,只是 IO 绑定操作现在是异步的。

var queueName = "testqueue";
var connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

var client = new ManagementClient(connectionString);

if (!await client.QueueExistsAsync(queueName).ConfigureAwait(false))
{
  await client.CreateQueueAsync(new QueueDescription(queueName)
  {
    MaxDeliveryCount = int.MaxValue,
    LockDuration = TimeSpan.FromMinutes(5),
    MaxSizeInMB = 5 * 1024,
    EnableBatchedOperations = true
  }).ConfigureAwait(false);
}