Windows Phone 7 and MissingMethodException

by Geert 27. August 2011 13:44

Recently I was building a demo application for Windows Phone 7 to show how it can be used with Catel. I created a new Windows Phone 7 project (for Mango) and tried to run the example. I hit a strange exception when a view model was constructed:

image

Now I thought: WTF?! I am pretty sure that this method exists, and the same code works for WPF, Silverlight and Windows Phone 7 (non-mango). After a few hours and trying, I decided to comment out the content of the RegisterViewModelServices method. What seemed: it’s the content of the method responsible for the MissingMethodException.

Let’s take a look at the content of that method:

   1: /// <summary>
   2: /// Registers the known view model services.
   3: /// </summary>
   4: /// <param name="serviceLocator">The service locator.</param>
   5: protected override void RegisterViewModelServices(IServiceLocator serviceLocator)
   6: {
   7:     // Logger already created, no IoC required
   8:     if (!_injectedServices.ContainsKey(typeof(ILogger)))
   9:     {
  10:         _injectedServices.Add(typeof(ILogger), Log);
  11:     }
  12:  
  13:     try
  14:     {
  15:         Log.Debug(TraceMessages.RegisteringDefaultServiceImplementationsForIoCContainer);
  16:  
  17: #if WINDOWS_PHONE
  18:         serviceLocator.RegisterTypeIfNotYetRegistered<ILocationService, LocationService>();
  19:         serviceLocator.RegisterTypeIfNotYetRegistered<IMessageService, MessageService>();
  20:         serviceLocator.RegisterTypeIfNotYetRegistered<INavigationService, Services.NavigationService>();
  21:         serviceLocator.RegisterTypeIfNotYetRegistered<IVibrateService, VibrateService>();
  22: #if WINDOWS_PHONE_MANGO
  23:         serviceLocator.RegisterTypeIfNotYetRegistered<IAccelerometerService, AccelerometerService>();
  24:         serviceLocator.RegisterTypeIfNotYetRegistered<ICameraService, CameraService>();
  25:         serviceLocator.RegisterTypeIfNotYetRegistered<ICompassService, CompassService>();
  26:         serviceLocator.RegisterTypeIfNotYetRegistered<IGyroscopeService, GyroscopeService>();
  27: #endif
  28: #elif SILVERLIGHT
  29:         serviceLocator.RegisterTypeIfNotYetRegistered<IMessageService, MessageService>();
  30:         serviceLocator.RegisterTypeIfNotYetRegistered<INavigationService, NavigationService>();
  31:         serviceLocator.RegisterTypeIfNotYetRegistered<IOpenFileService, OpenFileService>();
  32:         serviceLocator.RegisterTypeIfNotYetRegistered<IPleaseWaitService, PleaseWaitService>();
  33:         serviceLocator.RegisterTypeIfNotYetRegistered<ISaveFileService, SaveFileService>();
  34:         serviceLocator.RegisterTypeIfNotYetRegistered<IUIVisualizerService, UIVisualizerService>();
  35: #else // WPF
  36:         serviceLocator.RegisterTypeIfNotYetRegistered<IMessageService, MessageService>();
  37:         serviceLocator.RegisterTypeIfNotYetRegistered<INavigationService, NavigationService>();
  38:         serviceLocator.RegisterTypeIfNotYetRegistered<IOpenFileService, OpenFileService>();
  39:         serviceLocator.RegisterTypeIfNotYetRegistered<IPleaseWaitService, PleaseWaitService>();
  40:         serviceLocator.RegisterTypeIfNotYetRegistered<IProcessService, ProcessService>();
  41:         serviceLocator.RegisterTypeIfNotYetRegistered<ISaveFileService, SaveFileService>();
  42:         serviceLocator.RegisterTypeIfNotYetRegistered<IUIVisualizerService, UIVisualizerService>();
  43: #endif
  44:  
  45:         Log.Debug(TraceMessages.RegisteredDefaultServiceImplementationsForIoCContainer);
  46:     }
  47:     catch (Exception ex)
  48:     {
  49:         Log.Error(ex, TraceMessages.FailedToConfigureIoCContainer);
  50:  
  51:         throw new Exception(Exceptions.FailedToConfigureIoCContainer, ex);
  52:     }
  53: }

As you can see, all services that should be available per platform are registered so the user doesn’t have to do this. After commenting out all services and adding them one by one, it seems that all services regarding sensors were causing the MissingMethodException.

Then I thought: hey, they all share a common base class, maybe something is wrong with the base class for sensor services. After checking out all the references, it seems the following were referenced in Catel, but not in the demo app:

  • mscorlib.Extensions
  • Microsoft.Devices.Sensors
  • Microsoft.Xna.Framework
  • System.Device
  • System.Runtime.Serialization
  • System.Xml.Linq

I decided to add them all, even though I knew that those references should be included automatically (or at least be available since they are part of the phone framework). Again, no luck, still got the MissingMethodException.

At this time, I was already bald because I pulled all my hair out…

I double checked all references and made sure they were all pointing to the same files (right version, right runtime, etc). Still no luck.

Then I thought: hey, I did change something in the ISensorService recently. Instead of this definition:

   1: public interface ISensorService<TValueInterface, TEventArgs> 
   2:     where TEventArgs : EventArgs

I used this definition:

   1: public interface ISensorService<out TValueInterface, TEventArgs> 
   2:     where TEventArgs : EventArgs

I added the out because ReSharper told me I could define it as covariant. What seems: changing it back to a non-covarient type parameter solves the issue.

Now I am still now sure: is this a bug in my code, or is it a bug in the runtime? If you know, please let me know!

Tags:

C# | Windows Phone 7

Comments (1) -

Andrey
Andrey Russia
9/13/2011 9:46:47 PM #

Hi, I have the same problem exactly, if I make any parameter covariant or even have any field with covariant interface it throws exception and doesn't load dll.
That killed two days for me. :-(
I even couldn't think about such possibility.

Pingbacks and trackbacks (1)+

Comments are closed

About the Author

Geert van Horrik is an independent freelance software developer since January 1st, 2007. Since then he was been working on several projects from C++ to C# (WPF, Silverlight, ASP.NET, etc). Currently he loves to write his software using WPF (or Silverlight if WPF isn't an option).

Lately, Geert is spending a lot of time on Catel, a free open-source MVVM Framework for WPF and Silverlight. Actually, it's more than "just" an MVVM Framework, it's a complete application library!