Giter VIP home page Giter VIP logo

Comments (5)

mpodwysocki avatar mpodwysocki commented on June 2, 2024 3

@tiaan-lg yes, that would be a recommended approach. Please reopen this issue if this is still a problem.

from azure-notificationhubs-dotnet.

tiaan-lg avatar tiaan-lg commented on June 2, 2024 1

For anybody struggling with this, we solved it by passing in a MessageHandler from the
IHttpMessageHandlerFactory this prevents it from recreating the HTTP client and starving sockets

Example code where _httpMessageHandler is a static property

// if handler is not created, then create it
if (_httpMessageHandler == null)
{
    var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();

    var httpClientFactory = serviceProvider.GetService<IHttpMessageHandlerFactory>();

    _httpMessageHandler = httpClientFactory.CreateHandler();
}

var hub = new NotificationHubClient({{Connection String}}, {{Hub Path}}, new NotificationHubClientSettings
{
    MessageHandler = _httpMessageHandler
});

from azure-notificationhubs-dotnet.

tiaan-lg avatar tiaan-lg commented on June 2, 2024

I am also experiencing issues with using the hub client whereby it is exhausting sockets, what is the best practice for creating the and reusing the NotificationHubClient

from azure-notificationhubs-dotnet.

shabirjan avatar shabirjan commented on June 2, 2024

@mpodwysocki Can you please clarify if this approach is correct or not, we are adding the following code in the Constructor of our Controller.

private NotificationHubClient _nhClient;
public NotificationsController(CMLogger logger,
                                       IConfiguration config, IHttpMessageHandlerFactory httpMessageHandler)
        {
            _logger = logger;
            _config = config;
            _nhClient = new NotificationHubClient(_config["NotificationsHub:ConnectionString"],
             _config["NotificationsHub:HubName"],
             new NotificationHubClientSettings
             {
                 OperationTimeout = TimeSpan.FromSeconds(10),
                 MessageHandler = httpMessageHandler.CreateHandler()
             })
        }

from azure-notificationhubs-dotnet.

dzoech avatar dzoech commented on June 2, 2024

@tiaan-lg @mpodwysocki

Example code where _httpMessageHandler is a static property

Wouldn't this static HttpMessageHandler result in an also static HttpClientHandler for the whole lifetime of the application and therefore, might cause issues with DNS changes?

If I understand the inner workings of DefaultHttpClientFactory (which implements IHttpMessageHandlerFactory) correctly, calling CreateHandler() everytime a NotificationHubClient with scoped lifetime is created, should reuse the same handler pipeline. At least withing the lifetime of ActiveHandlerTrackingEntry. This reusing should mitigate the problem of socket exhaustion.

Please correct me if I understood something wrong.

from azure-notificationhubs-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.