Azure / azure-notificationhubs-dotnet

.NET SDK for Azure Notification Hubs
MIT License
71 stars 123 forks source link

Using NotificationHubClient from Azure function: System.Net.Sockets.SocketException #118

Closed morpheeen closed 4 years ago

morpheeen commented 4 years ago

Using NotificationHubClient from Azure function (the client is created on every function call) on high load fails with "System.Net.Sockets.SocketException: Cannot assign requested address" I believe this is because new HttpClient is created instead of using the HttpClientFactory and the ports are getting consumed very quickly (the known issue of using HttpClient inside Azure functions).

marstr commented 4 years ago

Howdy, @morpheeen. I've just responded to a very similar Stack Overflow question. Assuming you're not the original poster in SO, would you mind filing a support ticket so that we can look at your namespace/hub without doxing you?

morpheeen commented 4 years ago

Actually a colleague of mine was the one reporting the issue on stack overflow, I think the current implementation of the NotificationHubClient doesn't take into account this know issue: https://github.com/Azure/azure-functions-host/issues/1806 and causes socket exhaustion on high loads, the fix would be to use HttpClientFactory (https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests) to create new HttpClient.

stankovski commented 4 years ago

See comments in #107. You should be able to re-use HttpHandler.

Zapnologica commented 4 years ago

It seems we are also having a similar issue with using the notification hub on high volume loads. However, we got the error 'Unable to connect to the remote server.TrackingId:92cf019d-f3fc-4721-ac99-4e375f65f68b,TimeStamp:2020-04-08T06:56:24.9336294Z'

This continued for a good hour, and then I restarted the app, and then it started working again immediately.

morpheeen commented 4 years ago

See comments in #107. You should be able to re-use HttpHandler.

It's not working as expected... this is not a proper solution

stankovski commented 4 years ago

@morpheeen can you clarify what is not working and why it is not a proper solution?

vladTrenea commented 4 years ago

@stankovski thanks for the tip. We managed to overcome the problem in the end by injecting IHttpMessageHandlerFactory in our class and pass a handler to the NotificationHubClient in the constructor, in the NotificationHubClientSettings.

michaelmoneypenny commented 1 year ago

Is this still the recommended advice when using NotificationHubClient from an azure function? I see there is now the option of MessageHandler and HttpClient from NotificationHubSettings (trying to combat intermittent Socket exceptions/no known host issues)