簡體   English   中英

創建HttpNotificationChannel時獲取異常,但在逐步調試時可以正常工作

[英]Getting exception when creating HttpNotificationChannel, but works correctly when debugging step-by-step

我使用以下代碼創建推送通知通道。

問題是,當我執行代碼時,大多數時候(並非總是如此),此函數將拋出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;
}

由於在逐步調試時可以正常運行,因此無法找到問題。 任何想法?

問題在於打開通道是異步操作。 這就是存在ChannelUriUpdated事件的原因。 無法從函數中返回ChannelUri,因為在此函數結尾可能不可用。 它將在此塊中可用

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

它在調試時為您工作的原因是,在進入最后一行之前會觸發該事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM