gpt4 book ai didi

在 Azure 函数上集中使用时,Azure NotificationHubClient 会引发 SocketException

转载 作者:行者123 更新时间:2023-12-02 11:16:34 31 4
gpt4 key购买 nike

我们正在使用 Azure 函数通过 Azure NotificationHubClient 发送推送通知。该函数在实际环境中的高峰时段被大量调用,因此我们从NotificationHubClient.SendNotificationAsync()方法中抛出了大量的SocketException。

异常详细信息:

Microsoft.Azure.NotificationHubs.Messaging.MessagingException: Unexpected exception encountered TimeStamp:2020-03-27T04:14:35.4655705Z ---> System.Net.Http.HttpRequestException: Cannot assign requested address ---> System.Net.Sockets.SocketException: Cannot assign requested address
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Microsoft.Azure.NotificationHubs.NotificationHubClient.SendRequestAsync(HttpRequestMessage request, String trackingId, HttpStatusCode[] successfulResponseStatuses, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Microsoft.Azure.NotificationHubs.ExceptionsUtility.HandleUnexpectedException(Exception ex, String trackingId)
at Microsoft.Azure.NotificationHubs.ExceptionsUtility.TranslateToMessagingException(Exception ex, Int32 timeoutInMilliseconds, String trackingId)
at Microsoft.Azure.NotificationHubs.NotificationHubClient.SendRequestAsync(HttpRequestMessage request, String trackingId, HttpStatusCode[] successfulResponseStatuses, CancellationToken cancellationToken)
at Microsoft.Azure.NotificationHubs.NotificationHubClient.SendNotificationImplAsync(Notification notification, String tagExpression, String deviceHandle, CancellationToken cancellationToken)

我们创建NotificationHubClient,如下所示:

新的NotificationHubClient(connectionString,hubName,新的NotificationHubClientSettings
{
操作超时 = TimeSpan.FromSeconds(10)
});

我观察到,微软背后的实现是在每个新的NotificationHubClient实例化上实例化一个新的HttpClient - 这可能是问题所在(众所周知的套接字耗尽),但我不知道如何解决这个问题。 p>

有人遇到过同样的问题并设法以某种方式解决它吗?谢谢

最佳答案

最终我们通过注入(inject)成功解决了这个问题

    IHttpMessageHandlerFactory _httpMessageHandlerFactory;

在我们的类中并将其传递给构造函数中的 notificationHubClient:

    return new NotificationHubClient(connectionString, hubName, 
new NotificationHubClientSettings
{
OperationTimeout = TimeSpan.FromSeconds(10),
MessageHandler = _httpMessageHandlerFactory.CreateHandler()
});

关于在 Azure 函数上集中使用时,Azure NotificationHubClient 会引发 SocketException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60886453/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com