Print Page | Close Window

InvalidDataContractException

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3130
Printed Date: 25-Jan-2026 at 1:39pm


Topic: InvalidDataContractException
Posted By: splace
Subject: InvalidDataContractException
Date Posted: 07-Dec-2011 at 11:26am
I get the following exception during application start up.

Type 'DateRangeObject' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types 

This is occurring when trying to load the Metadata from the .ibmmx file. It is throwing the exception in a function called SerializationFns.RestoreDCS(...).

Do I have to annotate my classes with the [DataContract] attribute in Code First? I thought the [ProvideEntityAspect] generated the appropriate serialization code. Do you also have to annotate with the other attributes as well? [Binding] [Display] etc?

BTW it appears that DateRangeObject class is not the serialization file but is passed to the DataContract serializer as a KnownType.

Not sure if this helps but DateRangeObject is an abstract class.

Thanks for any help,
Sandy 



Replies:
Posted By: kimj
Date Posted: 08-Dec-2011 at 12:33pm
When the metadata file is loaded, we do deserialize its contents using the DataContractSerializer.  We're passing to the serializer the full list of "known" types, the same list we'd pass when doing other serialization/deserialization, including moving data between the client and server.  So, although it seems a bit odd since this type isn't part of the Code First model, you can think of it as the early warning, which would otherwise come later.
 
Any known type must be serializable:  this can mean marking it up with DataContract/DataMember attributes, or ensuring that it has a public constructor.
 
It's possible that this type doesn't need to be included as a known type, though.  Are you explicitly marking it as a known type?
 
As for the ProvideEntityAspect attribute, it does not actually mark up the CF entity class with DataContract/DataMember attributes.  The class must still be serializable, but these attributes aren't strictly required. 
 
You can find more information on known types and serialization here - http://drc.ideablade.com/xwiki/bin/view/Documentation/knowntypes - http://drc.ideablade.com/xwiki/bin/view/Documentation/knowntypes .
 
 
 
 



Print Page | Close Window