HttpNotificationChannel Open () throwing InvalidOperationException ("Could not open channel")

I am writing a Windows Phone 7 application that uses Push Notifications and has a class that is responsible for managing the interaction between MS notification servers and my service in the cloud. However, when I try to open a channel on my device, the HttpNotificationChannel throws an InvalidOperationException with the message "Could not open channel." According to MSDN I have to try to reopen the channel.

My code snippet for opening push notifications follows standard templates:

public class HttpNotification { private const string kChannelName = "MyApp.PushNotification"; private HttpNotificationChannel _Channel; public void Register() { try { _Channel = HttpNotificationChannel.Find(kChannelName); if (_Channel == null) { _Channel = new HttpNotificationChannel(kChannelName); InstallEventHandlers(); // This line throws _Channel.Open(); } else { InstallEventHandlers(); }; } catch (InvalidOperationException ex) { MessageBox.Show(string.Format("Failed to initialise Push Notifications - {0}", ex.Message)); }; } } 

I'm not sure exactly what MSDN means, "try opening the channel again." I wrapped the Open () call in try / catch and snoozing for 5 seconds between attempts, but this failed. I also tried the same approach throughout the method (i.e., did the HttpNotificationChannel.Find () call every time it throws) to no avail.

I know this is a bit vague, but I was wondering if anyone has any suggestions on this? The same code works flawlessly in the emulator, but does not work every time on my device itself even after installing and reinstalling my application. Given that this is my actual phone, I am a little restrained to make the hardware reset in the hope that it solves this problem and does not feel comfortable releasing the application to the market when this problem haunts me.

Update: an additional point, I use a channel that has not been verified, so a certificate is not installed for my cloud service.

Update # 2: In addition, I just tried deploying Microsoft Phone Push Recipe to my device, and also threw the same exception.

+6
c # windows-phone-7 push-notification mpns
source share
2 answers

So, from your comment, I understand that it works on your emulator, but not on your phone? Have you accidentally used the channel name in another / previous application?

The fact is that the reset emulator returns to it by default when it closes, and your phone does not. A specific channel name can be used by only one application. Therefore, if the channel name was used by another application on the same phone until it is registered in this application, and you can not access it from your application.

Conversely, an application can also register no more than one channel, so if one other name is associated with it, you cannot register a new one until you unregister the old one and reboot the device . There is also no way to request which channel is associated with your application.

In the end, when I got stuck in this loop, I changed the channel name and my ProductID applications registered in WMAppManifest.xml and it worked on me again.

 <App xmlns="" ProductID="{d57ef66e-f46c-4b48-ac47-22b1e924184b}" 

Update My computer crashed this weekend, thank goodness for WHS and backups. Anyway, below is my source code. I notice two differences.

  • First, I created a method called RepeatAttemptExecuteMethod() , to which I pass all the executable code as a delegate. 10 floats somewhere at the end is the number of repetitions. If you just repeated the .Open method every 5 seconds, the difference may be that I call the Find and New methods again ...

  • Another difference that I see is that my code assumes _appChannel.ChannelUri may be null. In this case, he expects the channel to raise an event, and then does the work related to the actual channel. But since your sample code does not do any work, I doubt it will be what you are looking for

     protected override void Load(PhoneApplicationPage parent) { Verkeer.Helper.ExternalResources.RepeatAttemptExecuteMethod(() => { _appChannel = HttpNotificationChannel.Find(CHANNELNAME); if (_appChannel == null) { _appChannel = new HttpNotificationChannel(CHANNELNAME); SetUpDelegates(); } else { SetUpDelegates(); //if (_appChannel.ChannelUri != null) this.NotificationChannel = _appChannel.ChannelUri; } if (_appChannel.ChannelUri != null) this.NotificationChannel = _appChannel.ChannelUri; else { try { _appChannel.Open(); } catch { } } BindToShellTile(); App.ViewModel.TrafficInfo.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(TrafficInfo_PropertyChanged); if (App.ViewModel.TrafficInfo.TrafficImage != null && this.NotificationChannel != null) { CreateTiles(); } },10); } private void BindToShellTile() { if (!_appChannel.IsShellTileBound && App.ViewModel.PanItemSettings.AutomaticallyUpdateTile) { Collection<Uri> ListOfAllowedDomains = new Collection<Uri> { new Uri("http://m.anwb.nl/") }; _appChannel.BindToShellTile(ListOfAllowedDomains); } } void TrafficInfo_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == "TrafficImage") { if (App.ViewModel.PanItemSettings.AutomaticallyUpdateTile && this.NotificationChannel != null) { CreateTiles(); } } } 
+6
source

@slaad .. here are a few things I would check if you hadn't tried them already:

  • Your actual device has the ability to connect to data, right? doh :)
  • How do you store an existing channel in isolated storage? Make sure your Find () is working and that you are not trying to recreate an existing pipe that results in an exception.
  • Check if your channel has problems with the domain name or certificates. Try this link
  • Check every step of the this process.

Sorry, don’t help too much.

0
source

All Articles