首页 > 解决方案 > 在 .NET 的 Azure 管理库中创建具有多个声明的 Azure 服务总线策略

问题描述

我将Azure 管理库用于 .NET和 Azure 服务总线。我的目标是创建一个包含Send and Listen声明的共享访问策略。我可以在 Azure 门户中执行此操作。

在 Azure 门户中创建的策略

IServiceBusNamespace serviceBus = ...
IBlank policyDefinition = serviceBus.AuthorizationRules.Define("MySendAndListenPolicy2");

IBlank我可以打电话WithListeningEnabled()WithSendingEnabled()。两者都返回一个IWithCreate,我只能从中创建规则 (即Create()CreateAsync()。似乎没有配置SendListen的选项。

如何使用 Azure 管理 .NET SDK创建包含发送侦听的策略?

标签: c#azureazureservicebus.net-standardazure-management-api

解决方案


我通过使用fluent API的内部对象实现了这一点。

// using Microsoft.Azure.Management.Fluent;
// using Microsoft.Azure.Management.ServiceBus.Fluent;
// using Microsoft.Azure.Management.ServiceBus.Fluent.Models;

string policyName = "ListenAndSendPolicy";

// Define the claims
IList<AccessRights?> rights = new List<AccessRights?>();
rights.Add(AccessRights.Send);
rights.Add(AccessRights.Listen);

// Create the rule
SharedAccessAuthorizationRuleInner rule = await serviceBus.AuthorizationRules.Inner.CreateOrUpdateAuthorizationRuleAsync(
    serviceBus.ResourceGroupName,
    serviceBus.Name,
    policyName,
    rights);

// Get the policy
policy = await serviceBus.AuthorizationRules.GetByNameAsync(policyName);

这成功创建了策略

在此处输入图像描述


推荐阅读