Cannot connect to Azure ServiceBus with Microsoft.Azure.ServiceBus

牧云@^-^@ 提交于 2019-11-29 08:21:33

问题


I have created a very simple console application that connects to Azure ServiceBus and sends one message. I tried the latest library from Microsoft (Microsoft.Azure.ServiceBus) but no matter what I do I just get this error:

No connection could be made because the target machine actively refused it ErrorCode: ConnectionRefused

I have tried exactly the same connection string in Service Bus Explorer and it does work just fine. Moreover I connected without problems using the older library from Microsoft (WindowsAzure.ServiceBus).

var sender = new MessageSender("endpoint", "topicName");
sender.SendAsync(new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject("test"))));

I tried with .NET Framework 4.6.2 and Core, same exception. I suspect there may be some differences in the default protocol that these libraries use, but I could not figure out that for sure.

P.S. Have tried the example from Microsoft docs but result is still the same exception


回答1:


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.




回答2:


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);


来源:https://stackoverflow.com/questions/50764285/cannot-connect-to-azure-servicebus-with-microsoft-azure-servicebus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!