Search code examples
c#wcfwindows-phone-7portable-class-librarymissingmethodexception

MissingMethodException when accessing portable library class in Windows Phone 7


I have a project that has the following structure:

  1. WP7 client (references 2)
  2. Portable library (shared)
  3. WCF Service (references 2)

ad 1) The client contains also service reference to service (3) with types reused from (2).
ad 2) Library contains data contracts (DataContract) used by both service and clients.

So far so good. All works perfectly on WVGA 512 MB emulator. But when I run it on real device or emulator 7.1. It throws MissingMethodException somewhere in constructing a class (uknown class).

I was able to narrow it down (after hours of commenting) to contracts in library (2). Whenever code touches any class in the portable library this exception is thrown. The client (1) contains reference (copy local == true). Don't know what else I can do.

The client is WP7.1, the portable library is .NET4+/SL4+/WP7+, and the service is .NET 4.5.

Why is this happening, how to use classes in portable library from within WP7 on a real device (and/or emulator 7.1).

Update: I've moved data contracts to client (1) project. But when they're accessed still the same exception TargetInvocationException (with inner MissingMethodException) is thrown. I'm puzzled. I suspect DataContract attribute now.

Update 2: It's not DataContract attribute either. The classes are derived from abstract generic class. I will try marking it with KnownType attribute, if it helps.

Update 3: So it's base class. I removed generic component of it, and still doesn't work. So I stripped away base class altogether, and it works now. But why it doesn't work? It works for service, it works for WVGA emulator, why not for device? I tried putting KnownType on base class, even on subclasses. Nothing works.


Solution

  • For anyone who might get this weird behavior. It was caused by covariance and contravariance indicators. I have this situation:

    public IMyInterface<in TClass>
    {
      void DoSomething(TClass value);
    }
    
    public MyClass : IMyInterface<MyClass>
    {
      // not important
    }
    

    It's literally caused by the in (contravariant), or out (covariant). Thanks, Microsoft. When I removed these, suddenly there was no problem where application didn't start at all before.