Azure通知中心的空引用异常

时间:2016-05-03 19:28:52

标签: c# android google-play entity-framework-6 azure-notificationhub

我一直在使用Azure通知中心和GCM向我的应用用户发送通知。在我将应用程序发布到Play商店之前,这一切都很有效。 现在,每当我尝试发布通知时,它都会出现500 Server错误。我不知道为什么会发生这种错误。也许应用程序没有拿起存储通知的RavenDB? 但看起来更像是服务无法收回在Hub上注册的用户。我真的不知道......任何帮助都会非常感激!

这是我在本地运行时的堆栈跟踪,它在发布时相同但不太详细:

"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;
}

1 个答案:

答案 0 :(得分:1)

您是否可以使用Service Bus Explorer并确认您的标签以&#34; user&#34;开头。我还看到您正在使用GetAllRegistrationsAsync API,建议仅用于调试目的。这是严格限制的API。

谢谢, Sateesh