Azure Notification Hub static instance or recreate
Asked Answered
L

1

6

What is the correct way to use the c# Azure Notification Hub Client?

We are using this in a very high usage server sending millions of pushes a day.

I would like to know if we should recreate the NotificationHubClient every time or if we should keep a static/singleton instance of it and then reuse that each time?

    /// <summary>
    /// Get refence to the azure hub
    /// </summary>
    /// <returns></returns>
    private static NotificationHubClient GetHub(NotificationHub nhub, bool enableTestSend = false)
    {          
        return NotificationHubClient.CreateClientFromConnectionString(nhub.EndPoint, nhub.HubName, nhub.AllowDiagnosticSend && enableTestSend);
    }

Currently, we recreate it every time we send a push notification. However, I know from personal experience we have had issues with the .net HTTP client and it not releasing tcp sockets fast enough. I was worried that this library could start having similar issues.

Layamon answered 8/4, 2020 at 9:39 Comment(0)
B
3

I would recommend to use the Singleton and reuse it rather than creating a new instance every time.

There is already a reported issue on GitHub where your current strategy (creating a new instance every time) fails on very high loads.

https://github.com/Azure/azure-notificationhubs-dotnet/issues/118

you can follow the below stackoverflow discussion as well.

Azure NotificationHubClient throws SocketException when used on Azure function intensively

Baucom answered 8/4, 2020 at 11:35 Comment(2)
Is there any documentation anywhere stating that a singleton instance in safe to use?Layamon
No there is no such document from Azure which states that singleton is safe to use here. you can refer below GitHub closed issues where people tried using singleton and they face some issues which was fixed by the Azure team. github.com/Azure/azure-notificationhubs-dotnet/issues/9Baucom

© 2022 - 2024 — McMap. All rights reserved.