发送更新时捕获到异常:System.NullReferenceException:对象引用未设置为对象的实例

Exception caught sending update: System.NullReferenceException: Object reference not set to an instance of an object

我向我的 xamarin 表单 UWP 应用程序添加了推送通知处理代码。

代码:

async Task InitRemoteNotificationAsync()
    {
        var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
        if (channel != null)
        {
            channel.PushNotificationReceived += OnPushNotificationReceived;
            Debug.WriteLine($"Received token:{channel.Uri}");
        }
    }

private void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
    {

        if (args.ToastNotification.Content.InnerText != null)
        {
            var msg = args.ToastNotification.Content.InnerText;
            Xamarin.Forms.MessagingCenter.Send<object, string>(this, MyProject.App.NotificationReceivedKey, msg);
        }
    }

我在我的代码中添加了空值检查。当我尝试从 http://pushtestserver.azurewebsites.net/wns/ 推送测试通知时出现以下异常。

Exception caught sending update: System.NullReferenceException: Object reference not set to an instance of an object. at WebRole1.WNS.WebForm1.PostToWns(String secret, String sid, String uri, String xml, String notificationType, String contentType) at WebRole1.WNS.WebForm1.btnDiyPush_Click(Object sender, EventArgs e)

截图:

在将消息发送到您的应用程序之前,异常显然是在 web 角色中抛出的。

您对此接收客户端应用程序无能为力。您的代码永远不会执行。该修复程序必须应用于 Web 角色。创建您自己的或使用实际有效的。