使用 Azure 服务总线发送消息时身份验证失败

Authentication failed sending a message with Azure Service Bus

我一直在研究如何使用 Azure 服务总线,并且一直在关注 Microsoft 的 this 教程。

我正处于尝试向队列发送消息的阶段,但出现身份验证错误:

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

还有一个内部异常,几乎相同的消息,这表明问题与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...]

可以看到此堆栈跟踪的其余部分 here

await queueClient.SendAsync(message);

以下代码(部分ConnectionString已用“********”涂黑):

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

我相信 ServiceBusConnectionStringQueueName 的设置是正确的,因为如果我更改这些值,我会在实例化 QueueClient 的那一行得到不同的错误。

我已尽可能地按照教程进行操作,但我无法克服此错误。我还搜索了 Google,但没能找到遇到此问题的其他人。我做错了什么?

这已确认是由于公司网络安全干扰了与 Azure "East US 2" 资源组位置中资源的传出 TLS 连接造成的。调整了一些防火墙规则后,一切正常。