WCF – Using the NetDataContractSerializer to share your type

DataContractSerializer

For those of your who are familiar with WCF you would no doubt know about the DataContractSerializer class, the DataContractSerializer does not include the CLR type information in the serialized XML, which allows for loose coupling between the client and server because they don’t have to share the same CLR types.

However, this also means you often have to re-create the same types on the client, though Visual Studio handles most of this for you when you add a new service reference to your project. However, rather than deserializing the original type, messages are deserialized into a new, but identical CLR type on the client.

NetDataContractSerializer

Whilst DataContractSerializer is used by WCF by default, there is another serializer which you can use if you wish to share the same CLR types between the client and server – the NetDataContractSerializer. The NetDataContractSerializer differs from the DataContractSerializer in that it includes the CLR type information in the serialized XML and can only be used if the same CLR types are serialized and deserialized at both ends.

You can use the NetDataContractSerializer on any type which are marked with the DataContractAttribute or SerializableAttribute, or types that implement the ISerializable interface.

UseNetDataContractSerializerAttribute

Here’s the attribute you need to replace the default DataContractSerializer with the NetDataContractSerializer, taken from Tim Scott’s blog post here:

public class NetDataContractOperationBehavior : DataContractSerializerOperationBehavior
{
    public NetDataContractOperationBehavior(OperationDescription operation) : base(operation)
    {
    }

    public NetDataContractOperationBehavior(
                OperationDescription operation,
                DataContractFormatAttribute dataContractFormatAttribute)
            : base(operation, dataContractFormatAttribute)
    {
    }

    public override XmlObjectSerializer CreateSerializer(
                Type type, string name, string ns, IList knownTypes)
    {
        return new NetDataContractSerializer(name, ns);
    }

    public override XmlObjectSerializer CreateSerializer(
                Type type, XmlDictionaryString name, XmlDictionaryString ns,
                IList knownTypes)
    {
        return new NetDataContractSerializer(name, ns);
    }
}

public class UseNetDataContractSerializerAttribute : Attribute, IOperationBehavior
{
    public void AddBindingParameters(OperationDescription description,
                                     BindingParameterCollection parameters)</pre>
{
}

public void ApplyClientBehavior(OperationDescription description,
System.ServiceModel.Dispatcher.ClientOperation proxy)
{
ReplaceDataContractSerializerOperationBehavior(description);
}

public void ApplyDispatchBehavior(OperationDescription description,
System.ServiceModel.Dispatcher.DispatchOperation dispatch)
{
ReplaceDataContractSerializerOperationBehavior(description);
}

public void Validate(OperationDescription description)
{
}

private static void ReplaceDataContractSerializerOperationBehavior(
OperationDescription description)
{
DataContractSerializerOperationBehavior dcsOperationBehavior =
description.Behaviors.Find();

if (dcsOperationBehavior != null)
{
description.Behaviors.Remove(dcsOperationBehavior);
description.Behaviors.Add(new NetDataContractOperationBehavior(description));
}
}
}

To use it, you can add it to a service method defined in the contract:

[OperationContract]
[UseNetDataContractSerializer]
Company SaveCompany(CompanyUpdater companyUpdater);