Elton Stoneman

  Home  |   Contact  |   Syndication    |   Login
  123 Posts | 0 Stories | 3755 Comments | 0 Trackbacks

News

Archives

Post Categories

[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 03, 2009 6:51 PM

Feedback

# re: Accessing Extended Data from IExtensibleDataObject 4/15/2009 7:39 PM An Phu
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?

# re: Accessing Extended Data from IExtensibleDataObject 4/15/2009 11:20 PM Elton
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


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

# re: Accessing Extended Data from IExtensibleDataObject 3/4/2010 3:20 AM gboy
I was thinking of a more concrete use for the approach.Thanks!Data Recovery Software|Guard Process

# re: Accessing Extended Data from IExtensibleDataObject 4/7/2010 6:39 AM Automatic Pool Cleaners
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.

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


# re: Accessing Extended Data from IExtensibleDataObject 5/5/2010 9:16 AM Gas Boilers
I really appreciate your sharing of this one. I was having trouble with it. Thanks!

# re: Accessing Extended Data from IExtensibleDataObject 9/21/2010 4:56 PM Dye
Good post. Thanks for taking the time and sharing.

# re: Accessing Extended Data from IExtensibleDataObject 12/8/2010 3:59 AM ipad scrapbook app
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.

# re: Accessing Extended Data from IExtensibleDataObject 3/12/2011 6:07 PM Inland Empire Carpet Repair
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.

# re: Accessing Extended Data from IExtensibleDataObject 3/14/2011 10:31 AM discount anabolics
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...

# re: Accessing Extended Data from IExtensibleDataObject 4/13/2011 11:16 AM Endless Summer
Very nice tutorial.. Thanks share this with me..



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

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

# re: Accessing Extended Data from IExtensibleDataObject 5/5/2011 1:07 PM Drug Test
I'm kind of jealous of the life I'm supposedly leading.

# re: Accessing Extended Data from IExtensibleDataObject 6/1/2011 9:59 AM Ian Filippini
Well this is very interesting indeed. Would love to read a little more of this. Great post. Thanks for the heads-up.

# re: Accessing Extended Data from IExtensibleDataObject 6/14/2011 11:12 AM Ian
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

# re: Accessing Extended Data from IExtensibleDataObject 6/15/2011 2:37 PM townhomes for sale vancouver
May god be grateful to this family and thanks keep on sharing more stuff

# re: Accessing Extended Data from IExtensibleDataObject 6/28/2011 1:17 PM Aussie kindle
You truly are a geek! Keep up the great work.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: