为什么HttpNotificationChannel.Open抛出ArgumentException()E_INVALIDARG?

时间:2011-02-04 18:04:42

标签: windows-phone-7 notifications argumentexception

这是我的代码。第一次运行它时一切正常,即HttpNotificationChannel.Find()返回null。

但是第二次运行它时,Find()返回正确的东西,但是当我进入Open()调用时,它会抛出异常。这真是奇怪,因为Open()不接受任何参数。

我做错了什么?

  public string ChannelName = "MyAppChannel";
  ...
  NotificationChannel = HttpNotificationChannel.Find(ChannelName);
  if (NotificationChannel == null)
  {
    NotificationChannel = new HttpNotificationChannel(ChannelName);
  }
  NotificationChannel.ChannelUriUpdated += new EventHandler(Channel_ChannelUriUpdated);
  NotificationChannel.HttpNotificationReceived += new EventHandler(NotificationChannel_HttpNotificationReceived);
  NotificationChannel.ErrorOccurred += new EventHandler(Channel_ErrorOccurred);
  NotificationChannel.Open();         // <-- Kaboom here, the 2nd time 

这是全文和堆栈:

System.ArgumentException: E_INVALIDARG
   at Microsoft.Phone.Notification.SafeNativeMethods.ThrowExceptionFromHResult(Int32 hr, Exception defaultException, NotificationType type)
   at Microsoft.Phone.Notification.HttpNotificationChannel.Open()
   at LiveShare.NotificationManager.Initialize()
   at LiveShare.App..ctor()
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
   at MS.Internal.TypeProxy.<>c__DisplayClass30.b__2a()
   at MS.Internal.TypeProxy.CreateInstance(UInt32 customTypeId)
   at MS.Internal.FrameworkCallbacks.CreateKnownObject(IntPtr nativeRootPeer, UInt32 customTypeId, String initializationString, IntPtr& nativePeer, UInt32 isCreatedByParser)
   at MS.Internal.FrameworkCallbacks.CreateUnknownObject(String assemblyName, String typeName, IntPtr nativeRootPeer, String initializationString, UInt32& customTypeId, UInt32& coreTypeId, UInt32& typeFlags, IntPtr& nativePeer)

2 个答案:

答案 0 :(得分:2)

正确的解决方案是,如果Open()成功,则不会致电Find()

答案 1 :(得分:1)

这看起来非常类似于去年4月CTP记录为described by Nick Harris的问题。

  

解决方案:如果您在模拟器启动时点击Debug后几乎立即尝试打开某个频道,则会出现此问题。解决方案很简单 - 在拨打电话前两分钟给模拟器。

如果不是这样的话,Silverlight Show上的一篇好文章涵盖了WP7股票报价应用中的推送通知。