如果 ServiceBus 主题尚不存在,则创建它

Create ServiceBus topic if it doesn't already exist

Microsoft 更新了他们的 .NET ServiceBus 客户端库,他们的文档目前在旧的 WindowsAzure.ServiceBus package and the new Microsoft.Azure.ServiceBus 包中分开。我喜欢这个新包,因为它更干净而且依赖性更少。在旧包中,我们有如下方法:

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

以编程方式创建主题的文档仍然使用旧包,代码如上。 NamespaceManager class 在新包中不可用,那么我怎样才能达到这个效果呢?

2022 年 1 月更新

Microsoft recommends to use ServiceBusAdministrationClient 在他们的最新包 Azure.Messaging.ServiceBus.

const string Topic = "<YourTopic>";    

// Create the topic if it doesn't exist
var adminClient = new ServiceBusAdministrationClient(ConnectionString);
if (!await adminClient.TopicExistsAsync(Topic))
    await adminClient.CreateTopicAsync(Topic);

与创建订阅类似。

感谢 Quan 的更新


原回答

在 Github Repo azure-service-bus-dotnet 上,他们解释了如何管理服务总线实体:

The standard way to manage Azure resources is by using Azure Resource Manager. In order to use functionality that previously existed in the .NET Framework Service Bus client library, you will need to use the Microsoft.Azure.Management.ServiceBus library. This will enable use cases that dynamically create/read/update/delete resources.

有一个关于如何使用这个库的示例:

您需要安装这些软件包:

  • Microsoft.Azure.Management.ServiceBus
  • Microsoft.Azure.Management.ResourceManager
  • Microsoft.IdentityModel.Clients.ActiveDirectory

如果您想创建一个主题,这对您来说很有趣。请注意,您无需检查主题是否存在。 Azure 资源管理器仅更新已存在的资源。

// On you've got the ServiceBusManagementClient
ServiceBusManagementClient sbClient = ...

sbClient.Topics.CreateOrUpdateAsync("resource group name", "namespace name", "topic name", 
    new Microsoft.Azure.Management.ServiceBus.Models.SBTopic());

如果您可以等待,还有一个未来的选择 - following issue. The options it will support 中描述的作为独立包的 NamespaceManager 也在问题中列出。

  • 获取 - 仅限于存在检查并返回元数据
  • GetRuntimeInformation(获取所有计数和最后状态,大概计数,10秒内准确)
  • GetQueueNames、GetTopicNames(列出实体名称)
  • 创建实体
  • 删除实体
  • 更新(需要更新哪些元数据的详细信息,可以在实施期间完成)
  • FindOrCreate(更新插入 - 队列不存在创建它)
  • 更新或创建(更新插入)

如果你喜欢 NamespaceManager 的轻松,那么值得关注这个问题。

在 .NET Core 中,您可以使用 ManagementClient 来执行此操作,与命名空间管理器相比更容易。

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

已接受的答案是在 2017 年,所以今天 1/13/2022 更新了我们如何在 Azure 服务总线上创建主题。

Microsoft 推荐在其最新包中使用 ServiceBusAdministrationClient Azure.Messaging.ServiceBus。

安装 NuGet 包:Azure.Messaging.ServiceBus

创建新主题的方法如下:

const string Topic = "<YourTopic>";    

// Create the topic if it doesn't exist
var adminClient = new ServiceBusAdministrationClient(ConnectionString);
if (!await adminClient.TopicExistsAsync(Topic))
    await adminClient.CreateTopicAsync(Topic);

与创建订阅类似。

此答案基于这篇文章https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-management-libraries#manage-using-service-bus-client-libraries