在System Phone中生成通道URI时出现'System.NullReferenceException'

时间:2013-07-08 09:22:20

标签: c#-4.0 windows-phone-7 windows-phone-8 push-notification

我正在使用Windows手机应用程序,我不知道为什么,但我没有获得HttpNotification频道的频道URI。

我收到“System.NullReferenceException”。我的代码在前一天工作,但相同的代码今天不起作用。

我的C#代码是:

    HttpNotificationChannel pushChannel;          
    string channelName = "ToastSampleChannel";

    // Try to find the push channel.
    pushChannel = HttpNotificationChannel.Find(channelName);

    // If the channel was not found, then create a new connection to the push service.
    if (pushChannel == null)
    {
        pushChannel = new HttpNotificationChannel(channelName, "www.contoso.com");

        // Register for all the events before attempting to open the channel.
        pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
        pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);

        // Register for this notification only if you need to receive the notifications while your application is running.
        pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);

        pushChannel.Open();

        // Bind this new channel for toast events.
        pushChannel.BindToShellToast();

    }
    else
    {
        // The channel was already open, so just register for all the events.
        pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
        pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);

        // Register for this notification only if you need to receive the notifications while your application is running.
        pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);

        // Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
        System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
        MessageBox.Show(String.Format("Channel Uri is {0}",
            pushChannel.ChannelUri.ToString()));

    }

我得到了这种类型的异常enter image description here

我不知道究竟是什么问题? 这是服务器问题还是其他什么?

1 个答案:

答案 0 :(得分:0)

根据我的经验,在退出应用之前检查通知渠道绑定状态。如果它没有绑定到tile和Toast,Microsoft推送通知服务将使其订阅无效,下次打开应用程序时将获得null channelUrl。