Sunday, March 11, 2012

Passing objects from/to WCF Service with "ExtensionData" field

Is there anyone here who can offer any assistance with this? It would be greatly appreciated!

Thanks!


I'm having the same issue. Help!?!
Hmm you guys are ahead of me. I'm only half way through my WCF book. =) Sorry can't help at this moment.

The reason the error is occurring is because the serializer tries to call the default constructor ofIExtensibleDataObject, which is internal to the class. It is also a sealed class, so you cannot override this behavior.

The answer is to create a custom javascript serializer like below. This will ignore the ExtensionData object when it is serialized to the client, effectively removing it from the object. The downfall is that you cannot use the ExtensionData once the object has been re-serialized from the client.


public class ExtensionDataObjectConverter : JavaScriptConverter{public override IEnumerable SupportedTypes {get {return new ReadOnlyCollection(new Type[] {typeof (ExtensionDataObject)}); } }public override IDictionary<string,object> Serialize(object obj, JavaScriptSerializer serializer) {return null; }public override object Deserialize(IDictionary<string,object> dictionary, Type type, JavaScriptSerializer serializer) {throw new Exception("The method or operation is not implemented."); }}

<system.web.extensions>

<scripting>

<webServices>

<jsonSerialization>

<converters>

<addname="ExtensionDataObjectConverter"

type="Utilities.Serialization.ExtensionDataObjectConverter"/>

</converters>

</jsonSerialization>

</webServices>

</scripting>

</system.web.extensions>

No comments:

Post a Comment