gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:18:24 28 4
gpt4 key购买 nike

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

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

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

最佳答案

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 存储库上 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());

关于c# - 如果 ServiceBus 主题尚不存在,则创建它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47761664/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com