【问题标题】:Create ServiceBus topic if it doesn't already exist如果 ServiceBus 主题尚不存在,则创建它
【发布时间】:2017-12-11 21:25:00
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# azure azureservicebus


    【解决方案1】:

    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,他们解释了如何管理服务总线实体:

    管理 Azure 资源的标准方法是使用 Azure Resource Manager。为了使用以前存在于 .NET Framework 服务总线客户端库中的功能,您需要使用 Microsoft.Azure.Management.ServiceBus 库。这将启用动态创建/读取/更新/删除资源的用例。

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

    您需要安装这些软件包:

    • 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());
    

    【讨论】:

    • 谢谢托马斯。我浏览了自述文件并没有发现“管理服务总线实体”等同于“创建/读取/更新主题和订阅”。 “资源组名称”是怎么回事?我假设这与我在 Azure 门户中看到的资源组相同,但是如果在 Azure 中转移资源,这样的代码不会更容易破坏吗?
    • 是的,它是 Azure 上资源组的名称。我想这取决于您是否使用多个资源组。也许你可以问另一个问题来处理它。
    【解决方案2】:

    在 .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

    【讨论】:

      【解决方案3】:

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

      Microsoft 建议在其最新包 Azure.Messaging.ServiceBus 中使用 ServiceBusAdministrationClient。

      安装 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

      【讨论】:

      • 您好,感谢您的更新。我不能接受一个问题的两个答案,因此我将添加此内容作为对 Thomas 答案的更新
      【解决方案4】:

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

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

      如果您喜欢NamespaceManager 的易用性,那么值得关注该问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-20
        • 2016-09-18
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 2014-05-21
        相关资源
        最近更新 更多