Building a Data Contract Serializer Behavior


In configuration there's a DataContractSerializer behavior that I can add to my service, but the class it uses is internal. How do I get the same behavior programmatically?

The DataContractSerializer behavior is a shortcut for setting the MaxItemsInObjectGraph and IgnoreExtensionDataObject options on endpoints. You can get a similar effect by looping over the operations in your service.

First, if the DataContractSerializer behavior is supplied as a service behavior, it applies to all of the non-metadata endpoints in your service. To check whether an endpoint is a metadata endpoint, we look for the following things:

  1. The endpoint has a ServiceMetadataBehavior in its description
  2. The endpoint service type is IMetadataExchange
  3. The endpoint contract name is IMetadataExchange in the namespace http://schemas.microsoft.com/2006/04/mex

Assuming that we've found a non-metadata endpoint, or the DataContractSerializer behavior is supplied as an endpoint behavior instead of a service behavior, we now scan through all of the operation descriptions on the endpoint contract. For each operation description we scan through all of its behaviors to find a DataContractSerializerOperationBehavior. The operation behavior is what stores the settings for the operation.

Finally, if the settings on the DataContractSerializerOperationBehavior were not already explicitly set, we change them to the values from the DataContractSerializer behavior. By doing this check, you can apply the behavior to an endpoint without overriding any values you set manually. This is the only step that you can't duplicate as the flag to tell where the value came from is internal. If you explicitly set the MaxItemsInObjectGraph or IgnoreExtensionDataObject options on an operation, then you'll need to explicitly exclude those operations in the behavior you write as well.

Comments (2)

  1. Kushal says:

    Does updating DataContractSerializerOperationBehavior needs to be done both on client on server ?

    I believe DataContractSerializerOperationBehavior properties should remain in sync while serializing and deserializing an object.

  2. There’s no requirement to keep the two in sync.  It depends on the specific properties being set.  It probably wouldn’t make sense for the client to send messages that the server cannot handle but it may make sense for the server to be able to handle messages that this client cannot generate.

Skip to main content