0

I am having a hard time getting my WCF service to serialize the data contract... I get a SerializationException when calling the service from the client, complaining about not expecting one of my derived classes. All of the similar questions I have found here all say that the fix is to use KnownType and ServiceKnownType like I am doing below, but that isn't working for me. Any ideas?

My service contract is:

[ServiceContract(...)]
[ServiceKnownType(typeof(DerivedClass))]
public interface IMyService
{
    [OperationContract]
    MainDataClass Process(List<MainDataClass> blah);
}

MainDataClass is:

[DataContract(...)]
[KnownType(typeof(DerivedClass))]
public class MainDataClass
{
    [DataMember]
    public List<BaseClass> Stuff { get; set; }
    ...
}

BaseClass is:

[DataContract(...)]
[KnownType(typeof(DerivedClass))]
public class BaseClass : ICloneable
{
    (lots of [DataMember] properties)
}

DerivedClass is:

[DataContract(...)]
public class DerivedClass : BaseClass
{
    (a few more [DataMember] properties)
}

The exception is:

System.Runtime.Serialization.SerializationException
Type 'DerivedClass' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or 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 the serializer.
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
at WriteArrayOfBaseClassToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )
at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
at WriteMainDataClassToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
coffee-converter
  • 967
  • 4
  • 13
  • Can you give a [complete code example](http://stackoverflow.com/help/mcve) that generates the exception, ideally by using [`DataContractSerializer` directly to serialize to a string](http://stackoverflow.com/questions/3238748/datacontractserializer-how-can-i-output-the-xml-to-a-string-as-opposed-to-a-f)? I created some compilable sample classes from your psuedocode but they serialized and deserialized successfully. – dbc Mar 14 '16 at 20:06
  • what about "BaseClass"? You should also include it in the interface declaration as ServiceKnownType. And I did not realize where DerivedClass fits on? Following your Interface, you use MainDataClass > BaseClass, no references to DerivedClass Try to remove DerivedClass from your ServiceKnownTypes. – Ricardo Pontual Mar 15 '16 at 14:38

0 Answers0