身份验证无法使用Azure Service Bus发送消息

时间:2018-07-25 14:33:31

标签: c# azure .net-core azureservicebus

我一直在使用Azure Service Bus,并一直遵循Microsoft的this教程。

我正在尝试将消息发送到队列的阶段,但是出现身份验证错误:

  

身份验证失败,因为远程方已关闭   运输流。

     Microsoft.Azure.ServiceBus.Core.MessageSender.d__53.MoveNext()上的

     在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在Microsoft.Azure.ServiceBus.RetryPolicy.d__18.MoveNext()      在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()      在Microsoft.Azure.ServiceBus.RetryPolicy.d__18.MoveNext()      在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在Microsoft.Azure.ServiceBus.Core.MessageSender.d__40.MoveNext()      在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在C:\ Repos \ ServiceBusTest \ Sender \ Program.cs:第27行的Sender.Program.d__0.MoveNext()中

还有一个内部异常,消息几乎相同,这表明该问题与TLS / SSL有关:

  

身份验证失败,因为远程方已关闭传输流。

     

at System.Net.Security.SslState.StartReadFrame(Byte [] buffer,Int32 readBytes,AsyncProtocolRequest asyncRequest)      在System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest asyncRequest)      在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()      在System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)      在System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult结果)      在System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)      在System.Threading.Tasks.TaskFactory 1.FromAsyncCoreLogic(IAsyncResult iar, Func 2 endFunction,Action 1 endAction, Task 1 promise,布尔值requireSynchronization处)      在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()      在
  [...片段...]

可以在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,但找不到其他遇到此问题的人。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

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