WCF Extensibility – Data Contract Resolver

This post is part of a series about WCF extensibility points. For a list of all previous posts and planned future ones, go to the index page.

Continuing on serialization, this post is about a new feature on the WCF serializers in .NET Framework 4.0, the DataContractResolver. Like the previous post on surrogates, there is already good documentation on MSDN, so this post will be short. But since I’ve seen an issue this week on the forums, I thought it’d be worth including the code here in this series.

In order to understand the data contract resolver, we need to take a step back and look at a common issue (and source of confusion) in WCF: known types. In order for WCF to be able to serialize or deserialize an object graph, it needs to know about all the objects in that graph. If the actual type of the object is the same as its declared type, then WCF already knows about it (the “baseInstance” member in the code below). If those types are different, then WCF doesn’t know by default about the type – in the case of the “derivedInstance” in the code below, WCF doesn’t know the MyDerived type, so it won’t serialize it unless we tell it that this type is known.

  1. public class MyBase { }
  2. public class MyDerived : MyBase { }
  4. [DataContract]
  5. public class MyType
  6. {
  7.     [DataMember]
  8.     public MyBase baseInstance = new MyBase();
  9.     [DataMember]
  10.     public MyBase derivedInstance = new MyDerived();
  11. }

There are many good descriptions on why we need the known types and how to set them (“official” MSDN documentation, Youssef Moussaoui’s MSDN blog, Mark Gravell’s Stack Overflow post, Richard Blewett’s blog, etc.) so I won’t go in detail here. But known types serves a purpose that we can declare, when the serialize is being created, which types are to be considered "valid” even though they’re not part of the object declaration. And the part in italic is important here – once the known type list is passed somehow to the serializer, it’s set and it cannot be changed during the serializer lifetime.

In the version 4.0 of the framework, WCF introduced a new concept, , the data contract resolver. Instead of defining the set of known types “statically” (either by specifying the types directly in a KnownTypeAttribute – or ServiceKnownTypeAttribute – or by specifying a method in those attributes which will be invoked when the serializer is being configured), the data contract resolver provides some hooks which allow us to specify, at the moment when the object is being serialized or deserialized, a mapping between the CLR type and the name / namespace in the XML which will be used to represent this “unknown” type. There is good documentation about this topic, including the MSDN reference page, the MSDN detail page, or Youssef Massaoui’s blog, so I won’t repeat that information here.

Class definition

  1. public abstract class DataContractResolver
  2. {
  3.     public abstract Type ResolveName(
  4.         string typeName, string typeNamespace,
  5.         Type declaredType, DataContractResolver knownTypeResolver);
  7.     public abstract bool TryResolveType(
  8.         Type type, Type declaredType, DataContractResolver knownTypeResolver,
  9.         out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace);
  10. }

When the serializer is set up with a DataContractResolver, during serialization, if the serializer finds out an object where the actual type doesn’t match the declared type, it will ask the resolver whether it can process it, by calling TryResolveType. If the resolver can do that, then the serializer will use the resulting name and namespace to write out the XML for that type in the type hint attribute (an attribute with local name “type” and namespace “http://www.w3.org/2001/XMLSchema-instance”. During deserialization, if a type hint attribute is found, then the serializer will call the ResolveName method on the resolver, where it can decide which type to use.

How to use a data contract resolver

Just like with the data contract surrogates shown in last post, the DataContractResolver can be passed as a parameter to the constructor of the WCF serializers (both the DataContractSerializer and the DataContractJsonSerializer). The example below shows one simple example of a resolver in action. Notice that all resolver implementations should delegate the call to the known type resolver parameter passed to it in case it can’t handle the type itself.

  1. public class MyResolver : DataContractResolver
  2. {
  3.     XmlDictionary dic = new XmlDictionary();
  4.     const string ResolverNS = "sma.ll/DCRS";
  6.     public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
  7.     {
  8.         if (typeNamespace == ResolverNS)
  9.         {
  10.             Type type = Assembly.GetExecutingAssembly().GetType(this.GetType().Namespace + "." + typeName, false);
  11.             if (type != null)
  12.             {
  13.                 return type;
  14.             }
  15.         }
  17.         return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
  18.     }
  20.     public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver,
  21.         out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
  22.     {
  23.         if (type == typeof(MyOtherType))
  24.         {
  25.             typeName = dic.Add(type.Name);
  26.             typeNamespace = dic.Add(ResolverNS);
  27.             return true;
  28.         }
  29.         else
  30.         {
  31.             return knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace);
  32.         }
  33.     }
  34. }
  36. [DataContract]
  37. public class MyType
  38. {
  39.     [DataMember]
  40.     public object Value;
  42.     public override string ToString()
  43.     {
  44.         return string.Format("MyType[Value={0}]", this.Value);
  45.     }
  46. }
  47. [DataContract]
  48. public class MyOtherType
  49. {
  50.     [DataMember]
  51.     public string Str;
  53.     public override string ToString()
  54.     {
  55.         return string.Format("MyOtherType[Value={0}]", this.Str);
  56.     }
  57. }
  59. class SimpleResolverTest
  60. {
  61.     public static void Test()
  62.     {
  63.         MyType obj = new MyType
  64.         {
  65.             Value = new MyOtherType { Str = "Hello World" },
  66.         };
  67.         MemoryStream ms = new MemoryStream();
  68.         DataContractSerializer dcs = new DataContractSerializer(
  69.             typeof(MyType), null, 65536, false, false, null, new MyResolver());
  70.         dcs.WriteObject(ms, obj);
  71.         Console.WriteLine("Serialized: {0}", Encoding.UTF8.GetString(ms.ToArray()));
  73.         ms.Position = 0;
  74.         object result = dcs.ReadObject(ms);
  75.         Console.WriteLine("Result: {0}", result);
  76.     }
  77. }

For setting the resolver in the context of WCF services, just like with the surrogates, you can use the DataContractResolver property on the DataContractSerializerOperationBehavior class. For an example of accessing this behavior, check the previous post.

Real world scenario: using dynamically-generated types

The scenario for the data contract resolver is when we want to delay the decision about known (safe?) types until it’s actually needed. I thought for a while but couldn’t think of an interesting scenario for this, until I stumbled upon this post on the forums, where the user wanted to use dynamic types (i.e., those created using the System.Reflection.Emit namespace). That’s a great example of a scenario where the “traditional” known type approach doesn’t work – the actual type may not even exist when the service / serializer is being created. So I created a sample with this scenario. The idea is passing an object between the client and the server which has a member of type System.Object, and the actual value of the field is of a dynamic type.

This is the holder object – nothing fancy here.

  1. [DataContract]
  2. public class MyHolder
  3. {
  4.     [DataMember]
  5.     public object MyObject { get; set; }
  7.     public override string ToString()
  8.     {
  9.         return string.Format("MyHolder[MyObject={0}]", this.MyObject);
  10.     }
  11. }

And in the service contract I wanted to have three scenarios: one where the client sends a request to the server with a dynamic type (which originally didn’t exist in the server) and receives the same type; one which the client just sends something new to the service, but receives a known type; and one which the server is the one who sends something back to the client that it doesn’t know yet.

  1. [ServiceContract(Name = "IDynamicContract", Namespace = "http://my.dynamic.contract")]
  2. public interface IDynamicContract
  3. {
  4.     [OperationContract]
  5.     MyHolder EchoHolder(MyHolder holder);
  6.     [OperationContract]
  7.     MyHolder GetHolder(string typeName, string[] propertyNames, object[] propertyValues);
  8.     [OperationContract]
  9.     string PutHolder(MyHolder holder);
  10. }

The server implementation is straightforward, although the GetHolder method uses a type builder library which I’ll mention later.

  1. public class DynamicService : IDynamicContract
  2. {
  3.     public MyHolder EchoHolder(MyHolder holder)
  4.     {
  5.         Console.WriteLine("In service, holder = {0}", holder);
  6.         return holder;
  7.     }
  9.     public MyHolder GetHolder(string typeName, string[] propertyNames, object[] propertyValues)
  10.     {
  11.         Console.WriteLine("In service, creating a type called {0}", typeName);
  12.         Type type = DynamicTypeBuilder.Instance.GetDynamicType(typeName);
  13.         object instance = Activator.CreateInstance(type);
  14.         for (int i = 0; i < propertyNames.Length; i++)
  15.         {
  16.             type.GetProperty(propertyNames[i]).SetValue(instance, propertyValues[i], null);
  17.         }
  19.         return new MyHolder { MyObject = instance };
  20.     }
  22.     public string PutHolder(MyHolder holder)
  23.     {
  24.         Console.WriteLine("In service, holder = {0}", holder);
  25.         return holder.ToString();
  26.     }
  27. }

And before I go on with more code, the usual disclaimer: this is a sample for illustrating the topic of this post, this is not production-ready code. I tested it for a few types and it worked, but I cannot guarantee that it will work for all scenarios (please let me know if you find a bug or something missing). Also, for simplicity sake it doesn’t have a lot of error handling which a production-level code would (i.e., in the server GetHolder / PutHolder method, in the type builder, etc.), and the type builder library only supports a fixed set of types, chosen for this sample only – in a real usage if the types would be created from some external storage such as a database (if they were fixed they would simply be written as “normal” ones instead of dynamic).

Now onto the interesting code. The DynamicTypeBuilder helper class is a singleton which knows how to build some pre-defined types. All the types are created in the same (dynamic) assembly so we can verify whether a certain type exists or not. The types are cached so that we only create a given type once.

  1. public class DynamicTypeBuilder
  2. {
  3.     internal const string AssemblyName = "ReflectionEmitAssembly";
  4.     private static DynamicTypeBuilder instance;
  5.     private static ModuleBuilder myModule;
  7.     private Dictionary<string, Type> typeCache = new Dictionary<string, Type>();
  8.     private Dictionary<string, Func<Type>> typeCreators = new Dictionary<string, Func<Type>>
  9.     {
  10.         { "Person", () => CreatePersonType() },
  11.         { "Address", () => CreateAddressType() },
  12.         { "Product", () => CreateProductType() },
  13.     };
  15.     static DynamicTypeBuilder()
  16.     {
  17.         AppDomain myDomain = AppDomain.CurrentDomain;
  18.         AssemblyName myAsmName = new AssemblyName(AssemblyName);
  19.         AssemblyBuilder myAssembly =
  20.             myDomain.DefineDynamicAssembly(myAsmName,
  21.                 AssemblyBuilderAccess.RunAndSave);
  23.         myModule =
  24.             myAssembly.DefineDynamicModule(myAsmName.Name,
  25.                myAsmName.Name + ".dll");
  26.     }
  28.     private DynamicTypeBuilder()
  29.     {
  30.     }
  32.     public static DynamicTypeBuilder Instance
  33.     {
  34.         get
  35.         {
  36.             if (instance == null)
  37.             {
  38.                 instance = new DynamicTypeBuilder();
  39.             }
  41.             return instance;
  42.         }
  43.     }
  45.     public Type GetDynamicType(string typeName)
  46.     {
  47.         Type result = null;
  48.         if (this.typeCache.ContainsKey(typeName))
  49.         {
  50.             result = this.typeCache[typeName];
  51.         }
  52.         else if (this.typeCreators.ContainsKey(typeName))
  53.         {
  54.             result = this.typeCreators[typeName]();
  55.             this.typeCache.Add(typeName, result);
  56.         }
  58.         return result;
  59.     }
  60. }

The types supported in this sample are simple DTO types, with a few properties, and a ToString implementation which will help showing their values. Each type is created using a helper method as shown below.

  1. private static Type CreatePersonType()
  2. {
  3.     return CreateType(
  4.         "Person",
  5.         new string[] { "name", "age" },
  6.         new string[] { "Name", "Age" },
  7.         new Type[] { typeof(string), typeof(int) });
  8. }
  10. private static Type CreateAddressType()
  11. {
  12.     return CreateType(
  13.         "Address",
  14.         new string[] { "street", "city", "zip" },
  15.         new string[] { "Street", "City", "Zip" },
  16.         new Type[] { typeof(string), typeof(string), typeof(string) });
  17. }
  19. private static Type CreateProductType()
  20. {
  21.     return CreateType(
  22.         "Product",
  23.         new string[] { "id", "name", "price" },
  24.         new string[] { "Id", "Name", "Price" },
  25.         new Type[] { typeof(int), typeof(string), typeof(decimal) });
  26. }

CreateType is where the types are actually created. Each type is decorated with the DataContractAttribute and has fields and properties defined based on the parameters passed to the method. I’ll omit the detailed implementation of CreateProperty and OverrideToString here (they’re in the project on the code gallery), as they aren’t really related to the topic on this post.

  1. private static Type CreateType(string typeName, string[] fieldNames, string[] propertyNames, Type[] propertyTypes)
  2. {
  3.     // public class Address
  4.     TypeBuilder typeBuilder =
  5.         myModule.DefineType(typeName, TypeAttributes.Public);
  7.     // Add [DataContract] to the type
  8.     CustomAttributeBuilder dataContractBuilder = new CustomAttributeBuilder(
  9.         typeof(DataContractAttribute).GetConstructor(Type.EmptyTypes),
  10.         new object[0]);
  11.     typeBuilder.SetCustomAttribute(dataContractBuilder);
  13.     List<PropertyBuilder> propertyBuilders = new List<PropertyBuilder>();
  14.     // Define fields and properties
  15.     for (int i = 0; i < fieldNames.Length; i++)
  16.     {
  17.         FieldBuilder field =
  18.             typeBuilder.DefineField(
  19.                 fieldNames[i],
  20.                 propertyTypes[i],
  21.                 FieldAttributes.Private);
  23.         propertyBuilders.Add(CreateProperty(typeBuilder, propertyNames[i], field));
  24.     }
  26.     // Override ToString
  27.     OverrideToString(typeBuilder, propertyBuilders.ToArray());
  29.     return typeBuilder.CreateType();
  30. }

Now onto the resolver. First: serialization (TryResolveType). When an instance of MyHolder is being serialized, if since the actual type of the MyObject property comes from the dynamic assembly (here we do a simple name comparison), we resolve that type using a constant namespace and the type name. If it doesn’t, then we delegate to the known type resolver which is passed to the method. During deserialization (ResolveName) we go the opposite way: if the namespace is our constant value, and we can load the dynamic type from our builder, then we return that type. If not, we again delegate the call to the resolver.

  1. public class DynamicTypeResolver : DataContractResolver
  2. {
  3.     const string ResolverNamespace = "http://dynamic/" + DynamicTypeBuilder.AssemblyName;
  4.     public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
  5.     {
  6.         if (typeNamespace == ResolverNamespace)
  7.         {
  8.             Type result = DynamicTypeBuilder.Instance.GetDynamicType(typeName);
  9.             if (result != null)
  10.             {
  11.                 return result;
  12.             }
  13.         }
  15.         return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
  16.     }
  18.     public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
  19.     {
  20.         if (type.Assembly.GetName().Name == DynamicTypeBuilder.AssemblyName)
  21.         {
  22.             XmlDictionary dic = new XmlDictionary();
  23.             typeName = dic.Add(type.Name);
  24.             typeNamespace = dic.Add(ResolverNamespace);
  25.             return true;
  26.         }
  27.         else
  28.         {
  29.             return knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace);
  30.         }
  31.     }
  32. }

The infrastructure pieces are done, we can now test to see if everything works out fine. First the server: in a simple self-hosted service, we replace the resolver in all operations in the endpoint contract. Nothing fancy here.

  1. static void Main(string[] args)
  2. {
  3.     string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
  4.     ServiceHost host = new ServiceHost(typeof(DynamicService), new Uri(baseAddress));
  5.     ServiceEndpoint endpoint = host.AddServiceEndpoint(
  6.         typeof(IDynamicContract), new BasicHttpBinding(), "");
  7.     foreach (OperationDescription operation in endpoint.Contract.Operations)
  8.     {
  9.         operation.Behaviors.Find<DataContractSerializerOperationBehavior>()
  10.             .DataContractResolver = new DynamicTypeResolver();
  11.     }
  13.     host.Open();
  14.     Console.WriteLine("Host opened, press ENTER to close");
  15.     Console.ReadLine();
  16.     host.Close();
  17. }

The client starts in a similar way: define the channel factory, update the DataContractSerializerOperationBehavior to set the resolver, then call the server operations. The Echo operation is shown below.

  1. string endpointAddress = "http://" + Environment.MachineName + ":8000/Service";
  2. var factory = new ChannelFactory<Server.IDynamicContract>(
  3.     new BasicHttpBinding(), new EndpointAddress(endpointAddress));
  4. foreach (var operation in factory.Endpoint.Contract.Operations)
  5. {
  6.     operation.Behaviors.Find<DataContractSerializerOperationBehavior>()
  7.         .DataContractResolver = new DynamicTypeResolver();
  8. }
  10. var proxy = factory.CreateChannel();
  11. Console.WriteLine("Created the proxy");
  13. object product = CreateProduct();
  14. Console.WriteLine("Product: {0}", product);
  16. Console.WriteLine("Calling the service passing the product...");
  17. MyHolder input = new MyHolder { MyObject = product };
  18. MyHolder output = proxy.EchoHolder(input);
  19. Console.WriteLine("Result: {0}", output);
  20. Console.WriteLine();

The PutHolder operation is similar to the first one: the client sends an object which doesn’t exist on the sever, and it’s then created by the resolver at the server process. The GetHolder is interesting in that in the client the type doesn’t exist yet, and we can show by enumerating the types in the dynamic assembly. But the operation call should work out just fine as well.

  1. Type[] dynamicTypes = product.GetType().Assembly.GetTypes();
  2. Console.WriteLine("Do we know about Person yet?");
  3. Console.WriteLine("All dynamic types so far");
  4. foreach (var dynamicType in dynamicTypes)
  5. {
  6.     Console.WriteLine("  {0}", dynamicType.Name);
  7. }
  9. Console.WriteLine("Retrieving a new type from the server");
  10. output = proxy.GetHolder("Person", new string[] { "Name", "Age" }, new object[] { "John Doe", 33 });
  11. Console.WriteLine("Output: {0}", output);
  12. Console.WriteLine("Type of output value: {0}", output.MyObject.GetType());
  13. Console.WriteLine("Is it on the same assembly as the other types? {0}",
  14.     output.MyObject.GetType().Assembly.Equals(address.GetType().Assembly));

And that’s it. The full code (including the methods not mentioned here) is on the code gallery sample (link to the code at the end of this post).

Final thoughts about data contract resolvers

Unlike with the data contract surrogates, resolvers can be used to change the name / namespace of primitive types. If you have, for example, a string object assigned to a member whose declared type is object, you can safely use a resolver to change the type name / namespace used in the type hint for the string. Another thing is that using a resolver makes the serialization slower than using the “standard” known types feature (since known types are static, they can be cached and the calls don’t need to be made all the time), so be aware that when using the resolver, the additional functionality comes at a price in terms of execution time.

Coming up

Wrapping up the serialization extensibility, with a short post on extensible data object.

[Code in this post]

[Back to the index]

Comments (4)

  1. Abhaye says:

    Hi, I seem to be facing an issue with deserializing the object in silverlight. I am using the generic resolver in my wcf service to resolve the dynamic known types and sending the entity object on a call back channel using duplex service. I get the message at the client end but the entity object is null. how can I overcome this deserializing issue.

  2. Abhaye, Silverlight doesn't have the data contract resolver feature implemented, so that won't work. What you'll likely need to do is to change the contract on the SL side to receive the object as a XElement, and from there do the deserialization manually.

  3. SeniorSafari says:

    Hi Carlos.

    Thanks a lot for the post, it's very useful.

    What if we have some classes that are inherited from base classes that are not decorated with [DataContract] attribute? could we write a custom Resolver for them ? if yes, could u please have an example?

    Thanks a lot man 🙂

  4. Catto says:

    Hey Now Carlos Figueira;

    This is a stellar article on the Data Contract Resolver in WCF. It is a great source of info on the ServiceKnownType attribute too.

    Thank you for the info!


Skip to main content