views:

829

answers:

1

I have a method in a WCF service which returns a complex type (myComplexResult), which includes as one of its members a List (Of Common.myBaseClass). I want this list to hold items which can variously be of type Foo.myClass1 and Bar.myClass2, both of which inherit from Common.myBaseClass. Note that all of these classes are defined in different assemblies.

The service throws this exception:

Type 'Foo.myClass1' with data contract name 'myClass1:http://mynamespace/foo/' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

Okay, so I realise that I need to declare myClass1 and myClass2 as known types of myBaseClass so that the DataContractSerializer knows what to do with them. I can't do the obvious thing and decorate the myBaseClass class with KnownType attributes for myClass1 and myClass2, since this would mean adding references to the Foo and Bar assemblies, which causes a circular dependency.

I was hoping to use declaredTypes in my config file and I tried this:

<system.runtime.serialization >
 <dataContractSerializer >
  <declaredTypes >
   <add type ="Common.myBaseClass, Common">
    <knownType type ="Foo.myClass1, Foo" />
    <knownType type ="Bar.myClass2, Bar" />
   </add>
  </declaredTypes>
 </dataContractSerializer>
</system.runtime.serialization>

That didn't seem to help, so I tried to add a KnownType attribute to myComplexResult:

  <DataContract(name:="myComplexResult", [namespace]:="http://mynamespace/coo/")&gt; _
    <KnownType(GetType(Foo.myClass1))> _
    Public Class myComplexResult
        <DataMember(name:="myList")> _
        Public myList As List(Of Common.myBaseClass)

But I'm still getting the same error. Help! What do I do?

A: 

My fault. I've re-tried and both of the solutions I posted above actually do work. I think this is a case of simply not updating the service reference on my test project before running the test - whoops!!!

elwy