嗨伙計們,我有一個問題,不幸的是我正在嘗試連接到 Azure EventHub,但我總是遇到如下例外:
System.Net.Sockets.SocketException (10060):連接嘗試失敗,因為連接方在一段時間后沒有正確回應,或者連接失敗,因為連接的主機沒有回應。在 Microsoft.Azure.Amqp.Transport.TransportStream.EndRead(IAsyncResult asyncResult) 在 Microsoft.Azure.Amqp.Transport.TransportStream.<>c__DisplayClass22_0.b__1(IAsyncResult a) 在 System.Threading.Tasks.TaskFactory
1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction,操作1 endAction, Task1 承諾, Boolean requiresSynchronization) --- 從先前位置結束堆疊跟蹤 --- 在 System.Net.Security.SslStream.g__InternalFillHandshakeBufferAsync|189_0[TIOAdapter](TIOAdapter adap, ValueTask1 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.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 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.FaultTolerantAmqpObject1.Microsoft.Azure.Amqp.Singleton 的 OnCreateAsync(TimeSpan 超時,CancellationToken cancelToken)1.GetOrCreateAsync(TimeSpan timeout, CancellationToken cancellationToken) at Microsoft.Azure.Amqp.Singleton1.Azure.Messaging.EventHubs.Amqp.AmqpConnectionScope.OpenManagementLinkAsync 的 GetOrCreateAsync(TimeSpan 超時,CancellationToken cancelToken)(TimeSpan operationTimeout,TimeSpan linkTimeout,CancellationToken cancelToken)在 Microsoft.Azure.Amqp.FaultTolerantAmqpObject1.OnCreateAsync(TimeSpan timeout, CancellationToken cancellationToken) at Microsoft.Azure.Amqp.Singleton1.GetOrCreateAsync(TimeSpan timeout, CancellationToken cancelToken) at Microsoft.Azure.Amqp.Singleton`1.GetOrCreateAsync(TimeSpan timeout, CancellationToken cancelToken) at Azure.Messaging.EventHubs.Amqp.AmqpClient.GetPropertiesAsync(EventHubsRetryPolicy retryPolicy, CancellationToken cancelToken) 在 Azure .Messaging.EventHubs.Amqp.AmqpClient.GetPropertiesAsync(EventHubsRetryPolicy retryPolicy, CancellationToken cancelToken) 在 Azure.Messaging.EventHubs.EventHubConnection.GetPropertiesAsync(EventHubsRetryPolicy retryPolicy, CancellationToken cancelToken) 在 Azure.Messaging.EventHubs.EventHubConnection.GetPartitionIdsAsync(EventHubsRetryCancelTokencancellationTokenretryToken) ) 在 Azure.Messaging.EventHubs.Consumer.EventHubConsumerClient。GetPartitionIdsAsync(CancellationToken cancelToken) 在 C:\Users\f.daquila\RiderProjects\AuditLogRecevierSample\AuditLogRecevierSample\Program.cs 的 Program.Main() 中的 Program.Main() 在 C:\Users\f.daquila\RiderProjects 的第 20 行\AuditLogRecevierSample\AuditLogRecevierSample\Program.cs: 第 41 行
我嘗試撥打電話的代碼是這樣的:
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 的網路是否有任何問題:

一切似乎都很好。我究竟做錯了什么?為什么我無法連接?
萬分感謝。
uj5u.com熱心網友回復:
持續超時最常見的連接問題是 AMQP over TCP (5671/5672) 所需的埠未打開。即使Test-NetConnection成功,這通常也是根本原因。
通過 WebSockets 將傳輸更改為 AMQP 通常會有所幫助,因為它將使用埠 443,并且可以在需要時通過代理進行路由。可以EventHubConsumerClientOptions在創建消費者時使用指定傳輸:
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屬性已棄用且已隱藏;它的存在只是為了向后兼容。我們建議使用EventBodywhich 為處理二進制資料提供額外的功能。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/531607.html
