我正在编写一个 Windows Phone 7 应用程序,它利用推送通知并有一个类负责管理 MS 通知服务器与我在云中的服务之间的交互。但是,当我尝试在我的设备上打开 channel 时,HttpNotificationChannel 抛出 InvalidOperationException 并显示消息“无法打开 channel ”。根据MSDN我应该尝试再次打开 channel 。
我打开推送通知的代码片段遵循以下标准模式;
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));
};
}
}
我不确定 MSDN 中“尝试再次打开 channel ”的确切含义。我已经将对 Open() 的调用包装在 try/catch 中,并在两次尝试之间暂停 5 秒,但它没有成功。我还在整个方法中尝试了相同的方法(即每次抛出时都调用 HttpNotificationChannel.Find())但无济于事。
我知道这有点含糊 - 但想知道是否有人对处理此问题有任何建议?同样的代码在模拟器中运行完美,但在我的实际设备上每次都失败,即使在卸载并重新安装我的应用程序之后也是如此。鉴于这是我的真实手机,我不太愿意进行硬件重置,希望它能解决这个问题,并且在这个问题困扰我的情况下,我不愿意将应用程序发布到市场。
更新:另外一点,我使用的是未经身份验证的 channel ,因此没有为我的基于云的服务安装证书。
更新 #2:此外,我刚刚尝试将 Microsoft Phone Push Recipe 部署到我的设备上,它也抛出了同样的异常。
最佳答案
因此,从您的评论中我了解到它确实可以在您的模拟器上运行,但不能在您的手机上运行,对吗? 您是否曾在其他/之前的申请中使用过 channel 名称?
问题是模拟器每次关闭时都会重置回默认状态,而您的手机不会。特定的 channel 名称只能由单个应用程序使用。因此,如果 channel 名称在同一手机上的另一个应用程序使用之前仍然注册到该应用程序,则您无法从您的应用程序访问它。
相反,一个应用程序也可以注册不超过一个 channel ,因此如果已经有一个与它关联的另一个名称,您将无法注册一个新 channel ,直到您取消注册旧 channel 并重新启动您的设备。也无法请求哪个 channel 与您的应用相关联。
最终,当我陷入这个循环时,我更改了 channel 名称和我在 WMAppManifest.xml 中注册的应用程序 ProductID,它再次从我这里开始工作
<App xmlns="" ProductID="{d57ef66e-f46c-4b48-ac47-22b1e924184b}"
更新 我的电脑这个周末死机了,感谢上帝的 WHS 和备份。 不管怎样,下面是我的源代码。我注意到两个不同之处。
首先,我创建了一个名为
RepeatAttemptExecuteMethod()
的方法,我将整个执行代码作为委托(delegate)传递给该方法。最后某处 float 的 10 是它必须重试的次数。如果您仅每 5 秒重试一次.Open
方法,不同之处可能在于我还再次调用了 Find 和 New 方法...我看到的另一个区别是我的代码假定 _appChannel.ChannelUri 可以为 null。在这种情况下,它会等待 channel 引发事件,然后执行与实际 channel 相关的工作。但是由于您的示例代码不执行任何此类工作,我怀疑它是否是您要找的东西
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(); } } }
关于c# - HttpNotificationChannel Open() 抛出 InvalidOperationException ("Failed to open channel"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5154135/