Bradley Uffner
Bradley Uffner

Reputation: 16991

Authentication failed sending a message with Azure Service Bus

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?

Upvotes: 1

Views: 2125

Answers (1)

Bradley Uffner
Bradley Uffner

Reputation: 16991

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.

Upvotes: 1

Related Questions