【问题标题】:Azure NotificationHubClient throws SocketException when used on Azure function intensivelyAzure NotificationHubClient 在 Azure 函数上密集使用时抛出 SocketException
【发布时间】:2020-07-08 05:15:18
【问题描述】:

我们使用 Azure 函数通过 Azure NotificationHubClient 发送推送通知。这个函数在实时环境的高峰时间被调用很多,结果我们得到了很多从 NotificationHubClient.SendNotificationAsync() 方法抛出的 SocketExceptions。

异常详情:

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:

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

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

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

【问题讨论】:

  • 你好,@vtrenea。您介意通过 Azure 门户开具支持票吗?工程团队希望进一步调查您的情况。
  • 我们遇到了非常相似的问题,但是我们的例外情况完全不同。 “无法连接到远程服务器。TrackingId:92cf019d-f3fc-4721-ac99-4e375f65f68b,TimeStamp:2020-04-08T06:56:24.9336294Z”但它与发送的大量推送有关,当我们重新启动应用程序,它会立即重新开始工作。

标签: azure azure-functions azure-notificationhub azure-hub


【解决方案1】:

最后我们通过注入解决了这个问题

    IHttpMessageHandlerFactory _httpMessageHandlerFactory;

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

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

【讨论】:

  • 你能详细说明你是怎么做到的吗?我们不能让这个工作?我们在 Windows 服务中运行并希望使用相同的“httpclient”实例
【解决方案2】:

通知中心有一个 RESTful API。你可以看到它here。如果您想要更多控制,您可以围绕您需要的方法编写自己的包装器,确保将 HttpClient 实例设置为 Azure 函数中的静态成员变量。

【讨论】:

  • 不能只使用“NotificationHubClient”的单例/静态实例。这样做安全吗?让图书馆的作者设计成我可以反复使用的方式。
  • 事实证明你不能。 Nuget 库每次都重新创建 HttpClient 对象..
猜你喜欢
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多