C# Azure EventHub连接异常

nwo49xxi  于 2022-11-17  发布在  C#
关注(0)|答案(1)|浏览(159)

Hy Guys,我有一个问题,不幸的是,我试图连接到Azure EventHub,但我总是得到一个异常,如:
异常错误(10060):连接尝试失败,原因是连接方在一段时间后未正确响应,或者建立的连接失败,原因是连接的主机未能响应。操作1 endAction, Task 1承诺,布尔值要求同步)---从前一个位置开始的堆栈跟踪结尾---位于System .NET.Security.SslStream.g__InternalFillHandshakeBufferAsync| 189_0[TIO适配器](TIO适配器适配,值任务1 task, Int32 minSize) at System.Net.Security.SslStream.ReceiveBlobAsync[TIOAdapter](TIOAdapter adapter) at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm) at System.Threading.Tasks.TaskToApm.End(IAsyncResult asyncResult) at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult) at Microsoft.Azure.Amqp.Transport.TlsTransport.HandleOpenComplete(IAsyncResult result, Boolean syncComplete) --- End of stack trace from previous location --- at Microsoft.Azure.Amqp.ExceptionDispatcher.Throw(Exception exception) at Microsoft.Azure.Amqp.AsyncResult.End[TAsyncResult](IAsyncResult result) at Microsoft.Azure.Amqp.AmqpObject.OpenAsyncResult.End(IAsyncResult result) at Microsoft.Azure.Amqp.AmqpObject.EndOpen(IAsyncResult result) at Microsoft.Azure.Amqp.Transport.TlsTransportInitiator.HandleTransportOpened(IAsyncResult result) at Microsoft.Azure.Amqp.Transport.TlsTransportInitiator.OnTransportOpened(IAsyncResult result) --- End of stack trace from previous location --- at Microsoft.Azure.Amqp.ExceptionDispatcher.Throw(Exception exception) at Microsoft.Azure.Amqp.AsyncResult.End[TAsyncResult](IAsyncResult result) at Microsoft.Azure.Amqp.Transport.AmqpTransportInitiator.ConnectAsyncResult.End(IAsyncResult result) at Microsoft.Azure.Amqp.Transport.AmqpTransportInitiator.<>c.<ConnectAsync>b__17_1(IAsyncResult r) at System.Threading.Tasks.TaskFactory 1.来自异步核心逻辑(IAsyncResult iar,函数2 endFunction, Action 1结束动作,任务1 promise, Boolean requiresSynchronization) --- End of stack trace from previous location --- at Azure.Messaging.EventHubs.Amqp.AmqpConnectionScope.CreateAndOpenConnectionAsync(Version amqpVersion, Uri serviceEndpoint, Uri connectionEndpoint, EventHubsTransportType transportType, IWebProxy proxy, Int32 sendBufferSizeBytes, Int32 receiveBufferSizeBytes, RemoteCertificateValidationCallback certificateValidationCallback, String scopeIdentifier, TimeSpan timeout) at Microsoft.Azure.Amqp.FaultTolerantAmqpObject 1.在Microsoft.Azure上创建异步(时间跨度超时,取消令牌取消令牌)。(时间跨度超时,取消令牌取消令牌)。消息传递。事件集线器。Amqp。Amqp连接范围。开放管理链接异步(时间跨度操作超时,时间跨度链接超时,取消令牌取消令牌)在Microsoft.Azure.Amqp.容错Amqp对象1.OnCreateAsync(TimeSpan timeout, CancellationToken cancellationToken) at Microsoft.Azure.Amqp.Singleton 1.获取或创建异步(时间跨度超时,取消令牌取消令牌)(时间跨度超时,取消令牌取消令牌)。消息传递。事件集线器。Amqp。Amqp客户端。GetPropertiesAsync(事件集线器重试策略重试策略,在Azure上取消令牌。消息传递。事件集线器。Amqp。Amqp客户端。获取属性异步(事件中心重试策略重试策略,取消令牌取消令牌)。消息传递。事件中心。事件中心连接。获取属性异步(事件中心重试策略重试策略,取消令牌取消令牌)。消息传递。事件中心。事件中心连接。获取分区ID异步在C:\Users\f. daquila\RiderProjects\AuditLogRecevierSample\AuditLogRecevierSample\Program. cs:C:\Users\f. daquila\RiderProjects\AuditLogRecevierSample\Program. cs:C:\用户\f.daquila\RiderProjects\AuditLogRecevierSample\Program. cs:C:\用户\f.daquila\RiderProjects\AuditLogRecevierSample\AuditLogRecevierSample\Program.cs:C:\用户\f.daquila\RiderProjects\审计日志接收器示例\审计日志接收器示例\Program. cs:C:\用户\f.daquila\RiderProjects\审计日志接收器示例\审计日志接收器示例\Program.cs:C:\用户\f. daquila\客户
我尝试调用的代码如下:

class Program
{
    private const string eventHubConnectionString = "<CONNECTION STRING>";
    private const string consumerGroup = "<GROUP NAME>";
    
    
    public static async Task Main()
    {
        try
        {
            await using var consumer = new EventHubConsumerClient(consumerGroup, new EventHubConnection(eventHubConnectionString));
            var startingPosition = EventPosition.Earliest;

            var partitionIds = await consumer.GetPartitionIdsAsync();

            if (partitionIds is not null && partitionIds.Any())
            {
                var partitionId = partitionIds.First();
                using var cancellationSource = new CancellationTokenSource();
                cancellationSource.CancelAfter(TimeSpan.FromSeconds(45));

                await foreach (var receivedEvent in consumer.ReadEventsFromPartitionAsync(partitionId, startingPosition, cancellationSource.Token))
                {
                    var body = Encoding.UTF8.GetString(receivedEvent.Data.Body.ToArray());

                    Console.WriteLine(body);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
}

我尝试使用powershell测试连接,看看网络是否有任何问题,如下所示:

一切似乎都很好。我做错了什么?为什么我不能连接?
非常感谢

mqkwyuun

mqkwyuun1#

持续超时最常见的连接问题是AMQP over TCP(5671/5672)所需的端口未打开。即使Test-NetConnection成功,这也常常是根本原因。
将传输更改为WebSockets上的AMQP通常会有所帮助,因为它将使用端口443,并且如果需要,可以通过代理路由。

var options = new EventHubConsumerClientOptions();
options.ConnectionOptions.TransportType = EventHubsTransportType.AmqpWebSockets;

await using var consumer = new EventHubConsumerClient(
    "<< CONSUMER GROUP >>",
    "<< CONNECTION STRING >>",
    "<< EVENT HUB NAME >>",
    options);

如果这不能解决问题,我建议您查看事件中心故障排除指南以了解进一步的步骤。
关于您的代码片段,还有其他一些值得注意的事情:

  • 没有理由显式地创建EventHubConnection,除非您要创建多个客户机并希望强制它们共享一个连接。
  • GetPartitionIdsAsync返回的值永远不会是null,并且始终至少有一个成员。无法创建没有分区的事件中心。
  • 不需要显式解码事件体的字节,使用receivedEvent.Data.EventBody.ToString()也会给予相同的结果。
  • 不推荐使用EventData.Body属性,该属性已隐藏;我们建议使用EventBody,它提供了处理二进制数据的附加功能。

相关问题