Connect to Azure Service Bus with Amqp over WebSockets using new Standard library

0

I am trying to send a message to my queue hosted in azure. My application communicates with the world through a proxy. I use the .Net Core 2.1 and the new the standard azure service bus nuget. I need to use Amqp over web sockets through the port 443. Unfortunately I have not succeed with that. Is there anybody who implemented such a thing? Can anybody provide a code snippet on how to achieve this?

Thanks in advance! Here is what I am trying:

var transportProvider = new AmqpTransportProvider();
var amqpSettings = new AmqpSettings
{ 
        RequireSecureTransport = true, TransportProviders = {transportProvider} 
};

var socketSettings = new WebSocketTransportSettings()
{
      Proxy = new WebProxy() {Address = new Uri("http://myproxy"), BypassProxyOnLocal = false},
      SubProtocol = "https",
      Uri = new Uri($"wss://{_azureConfiguration.NameSpace}"),
};

AmqpTransportInitiator transportIntInitiator = new AmqpTransportInitiator(amqpSettings, socketSettings);
var b = transportIntInitiator.ConnectAsync(TimeSpan.FromSeconds(30), new TransportAsyncCallbackArgs());

ServiceBusConnection s = new ServiceBusConnection($"sb://{_azureConfiguration.NameSpace}", TransportType.AmqpWebSockets);
s.TokenProvider = tokenProvider;
s.OperationTimeout = TimeSpan.FromSeconds(30);

var sender = new MessageSender(s, _azureConfiguration.QueueName);
string messageBody = $"Message";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
await sender.SendAsync(message);

The code gets following error: Unable to connect to the remote server. Inner exception details: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

StackTrace:

System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server ---> System.Net.Http.HttpRequestException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) at System.Threading.Tasks.ValueTask1.get_Result() at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Threading.Tasks.ValueTask1.get_Result() at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask1 creationTask) at System.Threading.Tasks.ValueTask1.get_Result() at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.WebSockets.WebSocketHandle.ConnectAsyncCore(Uri uri, CancellationToken cancellationToken, ClientWebSocketOptions options) at Microsoft.Azure.ServiceBus.Core.MessageSender.OnSendAsync(IList1 messageList) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\Core\MessageSender.cs:line 562 at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func1 operation, TimeSpan operationTimeout) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\RetryPolicy.cs:line 83 at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func1 operation, TimeSpan operationTimeout) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\RetryPolicy.cs:line 105 at Microsoft.Azure.ServiceBus.Core.MessageSender.SendAsync(IList1 messageList) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\Core\MessageSender.cs:line 252

c#
asp.net-core
azureservicebus
asked on Stack Overflow Dec 10, 2018 by Tilemachos Tzanakis • edited Dec 10, 2018 by Tilemachos Tzanakis

1 Answer

2

Make sure you are using Microsoft.Azure.ServiceBus and for functions Microsoft.Azure.WebJobs.Extensions.ServiceBus 3.x (I used 3.4.0 and 3.0.4 respectively). Use the standard var messageSender = new MessageSender(queueConnectionString, queueName, RetryPolicy.Default);

Just change your servicebus connectionstring to

Endpoint=sb://yadayada.servicebus.windows.net/;SharedAccessKeyName=SenderPolicy;SharedAccessKey=yadayada;TransportType=AmqpWebSockets

And it will use websockets/https and hence port 443. Sorry did not get the proxy part working but was under the impression that if it is setup at the network level, it will autodetect and the above should work. There is also a relay through option. If I get it working I will update, but I originally found this post just trying to get the 443/ampqwebsockets to work.

answered on Stack Overflow Apr 25, 2019 by MaryFromColorado • edited Apr 25, 2019 by MaryFromColorado

User contributions licensed under CC BY-SA 3.0