简体   繁体   English

来自Azure通知中心的Null参考异常

[英]Null Reference Exception From Azure Notification Hubs

I have been using Azure Notification Hubs along with GCM to send notifications to the users of my app. 我一直在使用Azure Notification Hub和GCM向我的应用程序的用户发送通知。 This was all working great until I published the app to the Play Store. 直到我将应用发布到Play商店之前,一切都很好。 Now it gives a 500 Server error whenever I try to post a notification. 现在,每当我尝试发布通知时,它都会显示500服务器错误。 I have no idea why this error is happening. 我不知道为什么会发生此错误。 Perhaps the app is not picking up the RavenDB where the notifications are stored? 也许应用程序没有选择存储通知的RavenDB? But it looks more like the service is not getting back users that are registered on the Hub. 但是,看起来该服务似乎并没有找回在集线器上注册的用户。 I really don't know... Any help would be so appreciated! 我真的不知道...任何帮助将不胜感激!

This is my stacktrace when run locally, it is the same but less detailed when published: 这是我在本地运行时的堆栈跟踪,它是相同的,但是发布时不那么详细:

"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

This is the Controller action: 这是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);
}

To get names from hub: 要从中心获取名称:

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

Could you use Service Bus Explorer and verify indeed you have tags starts with "user". 您能否使用Service Bus Explorer并确认确实有标记以“ user”开头。 And I also see you are using GetAllRegistrationsAsync API, which is recommend to use only for debugging purpose. 我还看到您正在使用GetAllRegistrationsAsync API,建议仅将其用于调试目的。 This is heavily throttled API. 这是严重限制的API。

Thanks, Sateesh 谢谢,Sateesh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM