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

标签 c# azure .net-core azureservicebus

我一直在考虑使用 Azure 服务总线,并且一直在关注 this来自 Microsoft 的教程。

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

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 所在的行上收到不同的错误code> 已实例化。

我已经尽可能严格地遵循了教程,但我无法克服这个错误。我还搜索了谷歌,但没有找到其他人遇到这个问题。我做错了什么?

最佳答案

经确认,这是由于公司网络安全干扰了与 Azure 的“East US 2”资源组位置中的资源的传出 TLS 连接造成的。调整一些防火墙规则后,一切都按预期工作。

关于c# - 使用 Azure 服务总线发送消息时身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51521541/

相关文章:

.net - 在 Windows Azure 上部署 MPI 应用程序

通过 CDN 直接 URL 故障转移策略的 Azure 存储

javascript - 如何从 Web 应用程序生成访问 token 并嵌入 token

c# - 如何在.NET Core中反序列化NetDataContractSerializer数据?

c# - 函数的监听器无法启动。为什么?

c# - 使用 MVVM lightMessenger 时,应用程序调用了为不同线程编码的接口(interface)

c# - 如何使用 C# 查询 NTP 服务器?

c# - .NET Core : IFileProvider. GetDirectoryContents 递归不起作用

c# - Windows CE 6.0 (OEM 5.37) 不显示沙漏光标

c# - 在可移植类库中使用外部 DLL