Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to Azure ServiceBus with Microsoft.Azure.ServiceBus

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

like image 420
Ilya Chernomordik Avatar asked Jun 08 '18 15:06

Ilya Chernomordik


People also ask

What is Service Bus in Azure?

Azure Service Bus is a fully managed enterprise message broker with message queues and publish-subscribe topics (in a namespace). Service Bus is used to decouple applications and services from each other, providing the following benefits: Load-balancing work across competing workers.

How do I connect to Azure Service Bus Explorer?

Connect using Shared access policy In order to connect to Azure Service Bus go to Azure portal, open the Service Bus namespace you want, then go to: Settings -> Shared access policies -> RootManageSharedAccessKey -> Primary connection string.

How do you get the Service Bus connection string from Azure portal?

Get the connection string To copy the primary connection string for your namespace, follow these steps: On the Service Bus Namespace page, select Shared access policies on the left menu. On the Shared access policies page, select RootManageSharedAccessKey.

What is Microsoft Service Bus messaging?

ServiceBus) - Azure for . NET Developers. Creates a ServiceBusReceiver instance that can be used for receiving and settling messages from a specific queue. It uses ServiceBusReceiveMode to specify how messages are received.

How do I troubleshoot Azure service bus issues?

Troubleshooting guide for Azure Service Bus 1 Connectivity, certificate, or timeout issues. ... 2 Issues that may occur with service upgrades/restarts. ... 3 Unauthorized access: Send claims are required. ... 4 Service Bus Exception: Put token failed. ... 5 Adding virtual network rule using PowerShell fails. ... 6 Next steps. ...

What is a messagingexception in Azure service bus?

If name resolution works as expected, check if connections to Azure Service Bus is allowed here MessagingException is a generic exception that may be thrown for various reasons. Some of the reasons are listed below. An attempt is made to create a QueueClient on a Topic or a Subscription.

Why can't my client connect to service bus?

Retry doesn't help. Client isn't able to establish a connection to Service Bus. Make sure the supplied host name is correct and the host is reachable. If your code runs in an environment with a firewall/proxy, ensure that the traffic to the Service Bus domain/IP address and ports isn't blocked.

Is there an easy way to migrate to Azure messaging service bus?

you're in the right direction. There's no easy migration tool/sample as they are different libraries dealing with the same service (Azure Service Bus). Is there more to it or am I good? I was in the same situation like you (trying to migrate to Azure.Messaging.ServiceBus which is now the recommended NuGet from Microsoft).


2 Answers

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.

like image 107
Sean Feldman Avatar answered Oct 17 '22 18:10

Sean Feldman


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);
like image 17
Ilya Chernomordik Avatar answered Oct 17 '22 18:10

Ilya Chernomordik