Geeks With Blogs

News
Elton Stoneman (@EltonStoneman) IT Consultant, integration specialist, @Microsoft MVP and @Pluralsight author.

[Source: http://geekswithblogs.net/EltonStoneman]

DataContract classes in WCF can be declared as implementing IExtensibleDataObject to provide in-built support for schema versioning. Any data contracts created through svcutil or Add Service Reference implement the interface, and it's good practice to implement it for any custom DataContract classes you write. On deserializing, elements which are not declared in the data contract are extracted by the DataContractSerializer and added to the ExtensionDataObject property, so unexpected data is not lost (see Vagif Abilov's blog post on backwards compatibility with IExtensibleDataObject).

Internally, the ExtensionDataMember holds a convoluted key-value collection of the unrecognised data members. If you want to inspect the contents of the collection it's a fiddly process as the members are non-public, and the types and interfaces of the members (ExtensionDataMember and IDataNode) are also non-public. The code below allows you to extract the value of an ExtensionDataMember given its name:

private object GetExtensionDataMemberValue(IExtensibleDataObject extensibleObject, string dataMemberName)

{

object innerValue = null;

PropertyInfo membersProperty = typeof(ExtensionDataObject).GetProperty("Members", BindingFlags.NonPublic | BindingFlags.Instance);

IList members = (IList)membersProperty.GetValue(extensibleObject.ExtensionData, null);

foreach (object member in members)

{

PropertyInfo nameProperty = member.GetType().GetProperty("Name", BindingFlags.NonPublic | BindingFlags.Instance);

string name = (string) nameProperty.GetValue(member, null);

if (name == dataMemberName)

{

PropertyInfo valueProperty = member.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance);

object value = valueProperty.GetValue(member, null);

PropertyInfo innerValueProperty = value.GetType().GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);

innerValue = innerValueProperty.GetValue(value, null);

break;

}

}

return innerValue;

}

 

Obviously this is fragile and isn't appropriate for production solutions, but it can be useful for unit testing or debugging WCF calls, or when you want to confirm the effects of having data contracts which are out of sync between the service and the consumer. Similar code could be used to get the count of extended data members to identify if the DataContract schema is not the correct version, and interestingly, the value and innerValue properties are writeable, so you could update the contents of the unrecognised members.

Posted on Friday, April 3, 2009 6:51 PM Code Snippet , WCF | Back to top


Comments on this post: Accessing Extended Data from IExtensibleDataObject

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
Interesting but I still don't see a need to inspect the contents of ExtensionDataObject.

There are better ways to debug WCF calls like the built-in WCF message logging.
http://msdn.microsoft.com/en-us/library/ms730064.aspx

As for unit testing, are you testing the service implementation or are you testing the WCF infrastructure?
Left by An Phu on Apr 15, 2009 7:39 PM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
I was thinking of a more concrete use for the approach: http://geekswithblogs.net/EltonStoneman/archive/2009/04/15/managing-concurrency-over-service-boundaries.aspx
Left by Elton on Apr 15, 2009 11:20 PM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
Interesting but I still don't see a need to inspect the contents of ExtensionDataObject.Good!Restore Deleted Files|进程保护
Left by luckd on Mar 04, 2010 3:18 AM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
I was thinking of a more concrete use for the approach.Thanks!Data Recovery Software|Guard Process
Left by gboy on Mar 04, 2010 3:20 AM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
Interesting post and thanks for sharing.It’s my first visit.I like very much your way of presentation.Each & every tips of your post are awesome.Thank you so much.keep blogging.
Left by Automatic Pool Cleaners on Apr 07, 2010 6:39 AM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
Very informative post, keep on sharing more informative posts like this. Keep up your good work, eagerly waiting for your next post.
Left by alberghi campo nell'elba on Apr 14, 2010 3:20 AM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
I really appreciate your sharing of this one. I was having trouble with it. Thanks!
Left by Gas Boilers on May 05, 2010 9:16 AM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
Good post. Thanks for taking the time and sharing.
Left by Dye on Sep 21, 2010 4:56 PM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
IExtensibleDataObject is very helpful in removal of data members. The IExtensibleDataObject interface is designed to support version round-tripping. And the WCF guidelines recommend enhancing all data contracts with support of IExtensibleDataObject interface.
Left by ipad scrapbook app on Dec 08, 2010 3:59 AM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
This blog gives the light in which I can observe the reality. This is very nice one and gives useful information. Thanks for this nice blog.
Left by Inland Empire Carpet Repair on Mar 12, 2011 6:07 PM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
This blog post is excellent, probably because of how well the subject was developped. I like some of the comments too though I would prefer we all stay on the suject in order add value to the subject...
Left by discount anabolics on Mar 14, 2011 10:31 AM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
Very nice tutorial.. Thanks share this with me..

Left by Endless Summer on Apr 13, 2011 11:16 AM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
I got lots of interesting information here. I appreciate this post.Keep it that way.Finally…have fun!
Left by Data Storage Backup Chicago on Apr 13, 2011 11:46 AM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
Excellent post. I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.
Left by houses for sale in johnson city on Apr 13, 2011 1:33 PM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
I'm kind of jealous of the life I'm supposedly leading.
Left by Drug Test on May 05, 2011 1:07 PM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
Well this is very interesting indeed. Would love to read a little more of this. Great post. Thanks for the heads-up.
Left by Ian Filippini on Jun 01, 2011 9:59 AM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
There are better ways to debug WCF calls and the built-in logging.Interesting WCF message but still do not see the need to inspect the contents of ExtensionDataObject.
Just Dreams Reviews
Left by Ian on Jun 14, 2011 11:12 AM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
May god be grateful to this family and thanks keep on sharing more stuff
Left by townhomes for sale vancouver on Jun 15, 2011 2:37 PM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
You truly are a geek! Keep up the great work.
Left by Aussie kindle on Jun 28, 2011 1:17 PM

# re: Accessing Extended Data from IExtensibleDataObject
Requesting Gravatar...
Nice one:-

http://senthilvijayalakshmi.blogspot.in/2013/03/what-is-purpose-of-extensiondataobject.html
Left by senthil on Mar 20, 2013 10:12 AM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net | Join free