【问题标题】:Failure to connect to Azure Service bus queue无法连接到 Azure 服务总线队列
【发布时间】:2019-03-10 14:36:58
【问题描述】:

我正在尝试监听一个 azure 服务总线队列 但是在初始化与队列的连接时出现异常。 我曾试图寻找出路,但收效甚微。 请注意,我使用的是 .NETCore 2.1。 这就是我初始化连接的方式:

// Initialize the connection to Service Bus Queue
_queueClient = QueueClient.CreateFromConnectionString(_interswitchQueueConnectionString, _interswitchQueueName, ReceiveMode.ReceiveAndDelete);

这是一个例外:

System.TypeInitializationException:类型初始化器 'Microsoft.ServiceBus.Messaging.Constants' 引发异常。 ---> System.TypeLoadException:无法加载类型“System.UriTemplate” 从程序集'System.ServiceModel,版本=4.0.0.0,文化=中性, PublicKeyToken=b77a5c561934e089'。 在 Microsoft.ServiceBus.Messaging.Constants..cctor() --- 内部异常堆栈跟踪结束 --- 在 Microsoft.ServiceBus.ServiceBusConnectionStringBuilder..ctor() 在 Microsoft.ServiceBus.Messaging.QueueClient.CreateFromConnectionString(字符串 connectionString、String 路径、ReceiveMode 模式)

使用 .net 框架时,相同的初始化工作正常。 我怎样才能在 .NetCore 中做到这一点? 请帮忙 提前致谢

【问题讨论】:

    标签: c# azure .net-core azure-servicebus-queues .net-core-2.1


    【解决方案1】:

    对于 .NET Core,您应该使用新的 .NET Standard Azure 服务总线客户端 Microsoft.Azure.ServiceBus。旧客户端 WindowsAzure.ServiceBus 是旧版客户端,仅适用于完整框架,建议继续使用。

    新客户没有工厂的概念。您负责创建实体客户端(QueueClientSubscriptionClientTopicClient)并管理连接。

    【讨论】:

    • 在使用Microsoft.Azure.ServiceBus / .NET Core 时如何使CloudConfigurationManager.GetSetting 工作?我似乎在任何地方都找不到直接的答案。
    • CloudConfigurationManager 是旧版配置 API。您应该研究替代方案。另外,不完全确定这与答案有什么关系?
    • 哦,是的,我想我之前累了,有点把两者归为一类。
    【解决方案2】:

    在 Dotnet Core 2.1 上你可以试试这个。

    var queueClient = new QueueClient(connectionString, entityPath, ReceiveMode.ReceiveAndDelete);
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢 Muhammed,我发现我必须使用 IQueueClient 才能完成这项工作,我必须使用 Microsoft.Azure.ServiceBus
    【解决方案3】:

    你也可以使用 MessagingFactory

    MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);
    
                queueClient = await factory.CreateMessageReceiverAsync(_entityPath, ReceiveMode.ReceiveAndDelete);
    

    【讨论】:

    • 这需要 WindowsAzure.ServiceBus,正如@Sean Feldman 所说,它仅适用于完整框架。尝试在 Dotnet Core 2.1 中使用 MessagingFactory 时出现 FileNotFoundException
    猜你喜欢
    • 1970-01-01
    • 2017-02-14
    • 2018-05-29
    • 2017-02-25
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    相关资源
    最近更新 更多