Azure / azure-notificationhubs-dotnet

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

[BUG] Random 502 errors when registering push tokens #197

Closed Tommigun1980 closed 3 years ago

Tommigun1980 commented 3 years ago

Describe the bug Sometimes when my ASP.NET Core server is calling the Azure Notification Hub to register or update a push token the notification hub server returns a 502/bad gateway error. This happens quite frequently.

Exception or Stack Trace AppInsights's error information for a call that returns 502 (with ids blanked out):

AppInsightsErrors

Code Snippet

public Task CreateOrUpdateInstallation(string installationId, string deviceToken, SharedConstants.PushPlatform pushPlatform, string[] additionalTags = null)
{
    var platform =
        pushPlatform == SharedConstants.PushPlatform.Apns ? NotificationPlatform.Apns :
        (pushPlatform == SharedConstants.PushPlatform.Fcm ? NotificationPlatform.Fcm : throw new UserArgumentException("Invalid push platform"));

    var tags = new List<string>()
    {
        installationId
    };
    if (additionalTags != null)
        tags.AddRange(additionalTags);

    var templates = new Dictionary<string, InstallationTemplate>(Templates.Count);
    foreach (var kvp in Templates)
        templates.Add(kvp.Key, kvp.Value[platform]);

    var installation = new Installation()
    {
        InstallationId = installationId,
        PushChannel = deviceToken,
        Platform = platform,
        Tags = tags,
        Templates = templates
    };

    // TODO: segment based on LoginSession.NotificationNamespaceIndex. must segment every 10M to a new notification hub namespace
    return this.Hub()?.CreateOrUpdateInstallationAsync(installation) ?? Task.CompletedTask; // error: this call will often return status code 502!!
}

Expected behavior CreateOrUpdateInstallationAsync should return status code 200. Instead, 502/bad gateway is often returned.

Setup (please complete the following information):

Additional context If I retry the call (i.e. run the above CreateOrUpdateInstallation(...) -> this.Hub()?.CreateOrUpdateInstallationAsync(...)) again, it seems to always succeed on the second try.

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

This is hurting me quite badly, so a quick resolution would be greatly appreciated! Thank you.

Tommigun1980 commented 3 years ago

On a second thought, this is probably better served by Azure support. Thank you.