WCF – Using the NetDataContractSerializer to share your type

Yan Cui

I help clients go faster for less using serverless technologies.

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 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


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 *