【问题标题】:Getting exception when creating HttpNotificationChannel, but works correctly when debugging step-by-step创建 HttpNotificationChannel 时出现异常,但在逐步调试时正常工作
【发布时间】:2014-04-16 07:38:44
【问题描述】:

我使用以下代码创建推送通知通道。

问题是当我执行代码时,大多数时候(并非总是),这个函数抛出System.NullReferenceException。但是,如果我设置断点并逐步调试它,它可以正常工作并返回有效的HttpNotificationChannel

private string AcquirePushChannel()
{
    HttpNotificationChannel currentChannel =  HttpNotificationChannel.Find("MyPushChannel");

    if (currentChannel == null)
    {
        currentChannel = new HttpNotificationChannel("MyPushChannel");
        currentChannel.Open();
        currentChannel.BindToShellTile();
        currentChannel.BindToShellToast();
    }

    currentChannel.ChannelUriUpdated += (s, e) =>
    {
        // Code here
    };
    currentChannel.ShellToastNotificationReceived += async (s, e) =>
    {
        // Code here
    };

    return currentChannel.ChannelUri.AbsoluteUri;
}

由于一步一步调试它工作正常,我无法找到问题所在。有什么想法吗?

【问题讨论】:

    标签: c# windows-phone-8 push-notification windows-phone mpns


    【解决方案1】:

    问题在于打开通道是一个异步操作。这就是存在 ChannelUriUpdated 事件的原因。无法从您的函数中返回 ChannelUri,因为它可能在此函数结束时不可用。它将在此块中可用

    currentChannel.ChannelUriUpdated += (s, e) =>
    {
        // here the channel uri is available as e.ChannelUri
    };
    

    它在调试时为您工作的原因是事件在您进入最后一行之前被触发。

    【讨论】:

    • 看来这就是问题所在。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多