WCF – Using the NetDataContractSerializer to share your type

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

Don’t reinvent the patterns. Catalyst gives you consistent APIs for messaging, data, and workflow with key microservice patterns like circuit-breakers and retries for free.

Try the Catalyst beta

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);

Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

2 thoughts on “WCF – Using the NetDataContractSerializer to share your type”

  1. Pingback: WCF — Data Contract version round-tripping using IExtensibleDataObject | theburningmonk.com

  2. Pingback: [??]?????WCF?? - c# - ?????

Leave a Comment

Your email address will not be published. Required fields are marked *