簡體   English   中英

來自Azure通知中心的Null參考異常

[英]Null Reference Exception From Azure Notification Hubs

我一直在使用Azure Notification Hub和GCM向我的應用程序的用戶發送通知。 直到我將應用發布到Play商店之前,一切都很好。 現在,每當我嘗試發布通知時,它都會顯示500服務器錯誤。 我不知道為什么會發生此錯誤。 也許應用程序沒有選擇存儲通知的RavenDB? 但是,看起來該服務似乎並沒有找回在集線器上注冊的用戶。 我真的不知道...任何幫助將不勝感激!

這是我在本地運行時的堆棧跟蹤,它是相同的,但是發布時不那么詳細:

"Message": "An error has occurred.",
"ExceptionMessage": "Value cannot be null.\r\nParameter name: source",
"ExceptionType": "System.ArgumentNullException",
"StackTrace": "   at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)\r\n   
at AcademicAssistantService.Controllers.NotificationController.<GetRecipientNamesFromNotificationHub>d__8.MoveNext() 
in C:\\Users\\Kenneth\\Documents\\College\\Semester 8\\AcademicAssistantService\\AcademicAssistantService\\Controllers\\NotificationController.cs:line 105

這是Controller動作:

// POST api/notification
public async Task<IHttpActionResult> Post([FromBody]Notification notification, String key)
{
    var notificationToSave = new Notification
    {
        NotificationGuid = Guid.NewGuid().ToString(),
        TimeStamp = DateTime.UtcNow,
        Message = notification.Message,
        SenderName = notification.SenderName
    };

    var recipientNames = await GetRecipientNamesFromNotificationHub(key);

    var recipientNamesString = CreateCustomRecipientNamesString(recipientNames);

    string notificationJsonPayload =
        "{\"data\" : " +
        "   {" +
        "   \"message\": \"" + notificationToSave.Message + "\"," +
        "   \"senderName\": \"" + notificationToSave.SenderName + "\"," +
        "   \"recipientNames\": \"" + recipientNamesString + "\"" +
        "   }" +
        "}";


    if (key == null)
    {
        var result = await _hubClient.SendGcmNativeNotificationAsync(notificationJsonPayload);

        notificationToSave.TrackingId = result.TrackingId;
        notificationToSave.Recipients = recipientNames;
    }
    else
    {
        foreach (string r in recipientNames)
        {
            if ((r != notification.SenderName))
            { 
                var result = await _hubClient.SendGcmNativeNotificationAsync(notificationJsonPayload, "user:" + r);
                notificationToSave.TrackingId = result.TrackingId;
                notificationToSave.Recipients = recipientNames;
            }
        }
    }

    await Session.StoreAsync(notificationToSave);

    return Ok(notificationToSave);
}

要從中心獲取名稱:

public async Task<List<string>> GetRecipientNamesFromNotificationHub(String key)
{
    var registrationDescriptions = await _hubClient.GetAllRegistrationsAsync(Int32.MaxValue);

    var recipientNames = new List<String>();
    foreach (var registration in registrationDescriptions)
    {
        if (registration is GcmRegistrationDescription)
        {
            var userName = registration.Tags
                                           .Where(t => t.StartsWith("user"))
                                           .Select(t => t.Split(':')[1].Replace("_", " "))
                                           .FirstOrDefault();
            userName = userName ?? "Unknown User";

            Conversation convo = db.Conversations.Find(key);

            foreach (User u in convo.Users)
            {
                if (u.Email == userName && !recipientNames.Contains(userName))
                {
                    recipientNames.Add(userName);
                }
            }
        }
    }
    return recipientNames;
}

您能否使用Service Bus Explorer並確認確實有標記以“ user”開頭。 我還看到您正在使用GetAllRegistrationsAsync API,建議僅將其用於調試目的。 這是嚴重限制的API。

謝謝,Sateesh

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM