Azure 通知中心静态实例或重新创建

Zap*_*ica 6 c# azure push-notification azure-notificationhub

使用 C# Azure 通知中心客户端的正确方法是什么?

我们在使用率非常高的服务器中使用它,每天发送数百万次推送。

我想知道我们是否应该NotificationHubClient每次都重新创建它,或者我们是否应该保留它的静态/单例实例,然后每次都重用它?

    /// <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);
    }
Run Code Online (Sandbox Code Playgroud)

目前,我们每次发送推送通知时都会重新创建它。然而,从个人经验来看,我们在 .net HTTP 客户端上遇到了问题,它释放 tcp 套接字的速度不够快。我担心这个图书馆可能会开始出现类似的问题。

小智 3

我建议使用 Singleton 并重用它,而不是每次都创建一个新实例。

GitHub 上已经报告了一个问题,您当前的策略(每次创建一个新实例)在非常高的负载下失败。

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

您也可以关注下面的 stackoverflow 讨论。

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