Search code examples
.netdatacontractserializerdatacontractservicebusknown-types

DataContract and KnownTypes for Inherited classes using Service Bus 1.0


Hi I have the following class hierarchy:

public class SuperJob{
}

public class JobA:SuperJob{
}

public class JobB:SuperJob{
}

When i try to deserialise a SuperJob i get

Expecting element 'SuperJob' from namespace 'http://schemas.datacontract.org/2004/07/...'.. Encountered 'Element'  with name 'JobA', namespace 'http://schemas.datacontract.org/2004/07/...'.

I have annotated my classes as follows:

[DataContract]
[KnownType(typeof(JobA))]
[KnownType(typeof(JobB))]
public class SuperJob{
}

[DataContract]
public class JobA:SuperJob{
}

[DataContract]
public class JobB:SuperJob{
}

Not sure what i am doing wrong? Any ideas how do i fix this?


Solution

  • Managed to fix this issue, there is a bug in the way the service bus instantiates the serialiser. looks like internally it instantiates the serialiser with JobA.getType(). So the best way forward is to create your own DataSerializer object which uses typeOf(JobA) and pass that in both when sending the message on to the bus and reading off the bus.