我一直在考虑使用 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.TaskFactory
1.FromAsyncCoreLogic(IAsyncResult iar, Func
2 endFunction, Action1 endAction, Task
1 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}");
}
}
}
}
我相信 ServiceBusConnectionString
和 QueueName
已正确设置,因为如果我更改这些值,我会在 QueueClient
所在的行上收到不同的错误code> 已实例化。
我已经尽可能严格地遵循了教程,但我无法克服这个错误。我还搜索了谷歌,但没有找到其他人遇到这个问题。我做错了什么?
最佳答案
经确认,这是由于公司网络安全干扰了与 Azure 的“East US 2”资源组位置中的资源的传出 TLS 连接造成的。调整一些防火墙规则后,一切都按预期工作。
关于c# - 使用 Azure 服务总线发送消息时身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51521541/