1
votes

I've been looking in to using the Azure Service Bus, and have been following this tutorial from Microsoft.

I'm at the stage where I'm trying to send a message to the queue, but I get an authentication error:

Authentication failed because the remote party has closed the transport stream.

at Microsoft.Azure.ServiceBus.Core.MessageSender.d__53.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.ServiceBus.RetryPolicy.d__18.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Azure.ServiceBus.RetryPolicy.d__18.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.ServiceBus.Core.MessageSender.d__40.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Sender.Program.d__0.MoveNext() in C:\Repos\ServiceBusTest\Sender\Program.cs:line 27

There is also an inner exception, with almost the same message, which suggests that the problem is related to TLS / SSL:

Authentication failed because the remote party has closed the transport stream.

at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest asyncRequest) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult) at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result) at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at
[...snip...]

The rest of this stack trace can be seen here.

on

await queueClient.SendAsync(message);

of the following code (parts of ConnectionString have been blacked out with "********"):

namespace CoreSenderApp
{
    using System;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Azure.ServiceBus;

    class Program
    {
        // Connection String for the namespace can be obtained from the Azure portal under the 
        // 'Shared Access policies' section.
        const string ServiceBusConnectionString = 
            "Endpoint=sb://le********est.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=aZDFcj****************v4=";
        const string QueueName = "testbus";
        static IQueueClient queueClient;

        static void Main(string[] args)
        {
            MainAsync().GetAwaiter().GetResult();
        }

        static async Task MainAsync()
        {
            const int numberOfMessages = 10;
            queueClient = new QueueClient(ServiceBusConnectionString, QueueName);

            Console.WriteLine("======================================================");
            Console.WriteLine("Press ENTER key to exit after receiving all the messages.");
            Console.WriteLine("======================================================");

            // Send Messages
            await SendMessagesAsync(numberOfMessages);

            Console.ReadKey();

            await queueClient.CloseAsync();
        }

        static async Task SendMessagesAsync(int numberOfMessagesToSend)
        {
            try
            {
                for (var i = 0; i < numberOfMessagesToSend; i++)
                {
                    // Create a new message to send to the queue
                    string messageBody = $"Message {i}";
                    var message = new Message(Encoding.UTF8.GetBytes(messageBody));

                    // Write the body of the message to the console
                    Console.WriteLine($"Sending message: {messageBody}");

                    // Send the message to the queue
                    await queueClient.SendAsync(message);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}");
            }
        }
    }
}

I believe that ServiceBusConnectionString and QueueName are set corectly, because if I change those values, I get a different error on the line where the QueueClient is instantiated.

I've followed the tutorial as closely as I can, but I can't get past this error. I also searched Google and haven't been able to find anyone else experiencing this problem. What am I doing wrong?

1
What version of .NET are you using. If you are using .NET 4.5 then you might want to try and set the security protocol to TLS to make sure SSL is not being used. You can do something like this: System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;Matt
.NET Core 2.0. I added that line at the start of the application, and there is no change.Bradley Uffner
There is nothing wrong with the code. It works like a charm. It is something related to security dependencies, please look into stackoverflow.com/questions/30664566/…Arunprabhu
It's starting to like that may be tied to corporate network / security policies interfering with TLS.Bradley Uffner
Some issue get the same error but the failures only occur when the server SSLProtocol being used is SslProtocols.Tls11 but not fail for SslProtocols.Tls12 or SslProtocols.Tls.Joey Cai

1 Answers

1
votes

This was confirmed to be caused by corporate network security interfering with the outgoing TLS connection to resources in the "East US 2" Resource group location of Azure. After adjusting some firewall rules, everything is working as expected.