Using custom DataContractResolver in WCF, to transport inheritance trees involving generics

Benson picture Benson · Mar 13, 2010 · Viewed 7.6k times · Source

I've got a WCF service, in which there are operations which accept a non-generic base class as parameter.

[DataContract]
class Foo
{ ... }

This base class is in turn inherited, by such generics classes as

[DataContract]
class Bar<T> : Foo
{ ... }

To get this to work, I'd previously have to register KnownTypes for the Foo class, and have these include all possible variations of Bar (such as Bar<string>, Bar<int> and even Bar<List<string>>).

With the DataContractResolver in .NET 4, however, I should be able to build a resolver which properly stores (and restores) the classes.

My questions:

  1. Are DataContractResolvers typically only used on the service side, and not by the client? If so, how would that be useful in this scenario?

  2. Am I wrong to write a DataContractResolver which serializes the fully qualified type name of a generic type, such as Bar`1[List`1[string, mscorlib], mscorlib] ? Couldn't the same DataContractResolver on the client side restore these types?

Answer

Marc Gravell picture Marc Gravell · Mar 13, 2010

I would expect that to work at both ends, but I'm not sure it is a great idea; it requires extra configuration, and won't work on Silverlight etc. But it'll probably work for "full" .NET with the same bits at each end.