1

我正在使用Windows Phone應用程序,但我不知道爲什麼,但我沒有獲取HttpNotification通道的通道URI。'System.NullReferenceException'在生成通道URI時發生在Windows Phone中

我得到「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

我不知道真正的問題是什麼? 這是服務器問題還是其他問題?

+0

任何人都可以知道答案嗎? – MansinhDodiya

回答

0

根據我的經驗,在退出應用程序之前檢查您的通知通道綁定狀態。如果未綁定到平鋪和烤麪包,Microsoft推送通知服務將使其訂閱無效,並且您下次打開應用時將獲得空的channelUrl。

+0

:謝謝你的回覆我檢查我的綁定method.it綁定到吐司像pushChannel.BindToShellToast(); 但問題沒有解決....有些時間它的工作一段時間它給錯誤我沒有得到有問題的問題 – MansinhDodiya

+0

您應該檢查通過使用'IsShellTileBound'和'IsShellToastBound'因爲綁定調用可能不會成功,併爲其他「結束」你的應用程序的方式(例如,未處理的異常,導航失敗或甚至從墓碑返回)。甚至還有其他一些情況,您的頻道會變得無效,例如:頻道在30天內無效,xml有效內容無效...所以最佳做法是在應用程序啓動時檢查您的頻道網址是否爲空,如果是,則必須等待在'ChannelUriUpdated'中獲取新的網址。 –

+0

:謝謝重播devid我已經嘗試過,沒有運氣yet.IT返回相同的錯誤。我也等待chanaluriupdated,但沒有輸出。 – MansinhDodiya

相關問題