如何在 Azure Function 2.0 版中访问 Microsoft.ServiceBus.NamespaceManager 或 QueueDescription 实例

How to access Microsoft.ServiceBus.NamespaceManager or a QueueDescription instance in Azure Function version 2.0

我有一个由服务总线队列触发的 azure 函数。我可以访问消息,但我也在尝试使用 Microsoft.ServiceBus.QueueDescription; 的实例访问有关队列的信息;但是,我在 azure 函数中使用 Microsoft.ServiceBus 命名空间时遇到困难。我最初收到一个错误

Microsoft.ServiceBus: The type initializer for
'Microsoft.ServiceBus.Messaging.Constants' threw an exception. Microsoft.ServiceBus: 
Could not load file or assembly 
'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 
The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.

所以我将 System.ServiceModel 的所有库添加到项目中;但是,在无法找到缺少的依赖项的测试中出现了类似的问题。

我是否应该使用不同的方式为触发函数管道的队列获取 QueueDescription 实例?我目前在项目中有以下库

Microsoft.Azure.WebJobs.Extensions.ServiceBus (3.0.4)
WindowsAzure.ServiceBus (5.2.0)

如果包含的任一服务总线库能够访问 QueueDescription 实例,您有什么想法吗?

谢谢

由于 .NET Core 兼容包 Microsoft.Azure.ServiceBus 不再支持 NamespaceManager(当在 WebJobs 或 Functions 中使用服务总线时,它是 Microsoft.Azure.WebJobs.Extensions.ServiceBus 的依赖项),包 Microsoft.Azure.Management.ServiceBus.Fluent 和关联公司必须使用。

我在我的 Functions with Managed Identity 中做到了,但 AzureCredentialsFactory 也支持其他形式的身份验证:

...
    // some magic that determines subscriptionId, resourceGroupName & sbNamespaceName
...
    var credentials = SdkContext.AzureCredentialsFactory.FromMSI(new MSILoginInformation(MSIResourceType.VirtualMachine), AzureEnvironment.AzureGlobalCloud);
    var azure = Azure
            .Configure()
            .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
            .Authenticate(credentials)
            .WithSubscription(subscriptionId);

    var sbNamespace = azure.ServiceBusNamespaces.GetByResourceGroup(resourceGroupName, sbNamespaceName);
    var queues = sbNamespace.Queues.List();
...