Cannot connect to Azure ServiceBus with Microsoft.Azure.ServiceBus

青春壹個敷衍的年華 提交于 2019-11-30 09:05:57

The old client supported ConnectivityMode using TCP, HTTP, HTTPS, and AutoDetect. ServiceBus Explorer is using AutoDetect, trying TCP first and then failing over to HTTPS, regardless of the TransportMode you were using (SBMP or AMQP).

With the new client this has changed. TransportMode now combines both options and offers Amqp (AMQP over TCP) or AmqpWebSockets (AMQP over WebSockets). There's no AutoDetect mode. You will have to create your clients and specify TransportType as AmqpWebSockets to bypass blocked TCP port 5671 and instead use port 443.

It seems that the documentation is lacking a lot on how to connect using HTTPS (Amqp over WebSockets) but after some help from Sean Feldman in the accepted answer I managed to connect. Here is the code that I used if someone is interested:

var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(
    "RootManageSharedAccessKey", // SharedAccessKeyName
    "SomeToken");

var sender = new MessageSender(
    "sb://mydomain.servicebus.windows.net/",
    "topicName",
    tokenProvider,
    TransportType.AmqpWebSockets);

Or a variant that let's you have the whole connection string in one piece

var builder = new ServiceBusConnectionStringBuilder("YouConnectionString");

var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(
    builder.SasKeyName,
    builder.SasKey);

var sender = new MessageSender(
    builder.Endpoint,
    "TopicName",
    tokenProvider,
    TransportType.AmqpWebSockets);

It is actually possible to use ConnectionString directly but then it has to be augmented to use the right protocol.

var sender = new MessageSender("TransportType=AmqpWebSockets;Endpoint=...", "TopicName")

Or the version that allows to embed EntityPath into the ConnectionString

var connectionBuilder = new ServiceBusConnectionStringBuilder("EntityPath=MyTopic;TransportType=AmqpWebSockets;Endpoint=...")
var sender = new MessageSender(connectionBuilder);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!