Search code examples
c#wpfserializationcatel

Type <Type> with data contract name <contract name> is not expected


I am using Catel Framework in a Desktop WPF app and when I try to save a model to a file I get the following error.

Type 'GeoChemicalFuncsCS.Core.Models.BoilingModel' with data contract name 'BoilingModel:http://schemas.datacontract.org/2004/07/GeoChemicalFuncsCS.Core.Models' is not expected. Consider using a DataContractResolver 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 DataContractSerializer.

I tried adding the KnownType and ServiceKnownType Tag to the model, but I get:

KnownType could not be found.

or

ServiceKnownType could not be found.

And I also tried WarmingUp the SerializationFactory in the ViewModel:

var typesToWarmup = new Type[] { typeof(BoilingModel) };
SerializationFactory.GetXmlSerializer().Warmup(typesToWarmup);

but nothing changed.

Does anyone have any suggestion? What can I do at this point?

Thanks Saul Hidalgo.


Solution

  • After some work I finally solved it.

    Problem was originated because I had a collection which contained 2 types of objects (both serializable). I tried to generalize it creating a collection of "object", and when I needed to use it I casted it.

    Well, serializer engine found the type "object", and when he tried to serialize, he found it really was a BoilingModel.

    Solution was just doing an interface, and now both serializable types implement that interface.

    I hope it helps someone else with the same problem.

    Regards Saul.