DataContractSerializerOperationBehavior Class
.NET Framework Class Library
DataContractSerializerOperationBehavior Class

Represents the run-time behavior of the DataContractSerializer.

Namespace:  System.ServiceModel.Description
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
Visual Basic (Declaration)
Public Class DataContractSerializerOperationBehavior _
    Implements IOperationBehavior, IWsdlExportExtension
Visual Basic (Usage)
Dim instance As DataContractSerializerOperationBehavior
C#
public class DataContractSerializerOperationBehavior : IOperationBehavior, 
    IWsdlExportExtension
Visual C++
public ref class DataContractSerializerOperationBehavior : IOperationBehavior, 
    IWsdlExportExtension
JScript
public class DataContractSerializerOperationBehavior implements IOperationBehavior, IWsdlExportExtension

The DataContractSerializerOperationBehavior enables control of DataContractSerializer options, such as the MaxItemsInObjectGraph and IgnoreExtensionDataObject properties. Some properties can only be set using the constructor of the class. In that case, the DataContractSerializer can be replaced using the CreateSerializer method. For more information about data contracts, see Using Data Contracts.

For more information about using the DataContractSerializerOperationBehavior to replace the IDataContractSurrogate returned by the DataContractSurrogate property, see Data Contract Surrogates.

The following example finds the DataContractSerializerOperationBehavior in the collection of behavior for an operation and resets the MaxItemsInObjectGraph and IgnoreExtensionDataObject properties.

Visual Basic
Private Sub DataContractBehavior() 
    Dim b As New WSHttpBinding(SecurityMode.Message)
    Dim baseAddress As New Uri("http://localhost:1066/calculator")
    Dim sh As New ServiceHost(GetType(Calculator), baseAddress)
    sh.AddServiceEndpoint(GetType(ICalculator), b, "")

    ' Find the ContractDescription of the operation to find.
    Dim cd As ContractDescription = sh.Description.Endpoints(0).Contract
    Dim myOperationDescription As OperationDescription = cd.Operations.Find("Add")

    ' Find the serializer behavior.
    Dim serializerBehavior As DataContractSerializerOperationBehavior = _
    myOperationDescription.Behaviors.Find _
    (Of DataContractSerializerOperationBehavior)()

    ' If the serializer is not found, create one and add it.
    If serializerBehavior Is Nothing Then
        serializerBehavior = New DataContractSerializerOperationBehavior(myOperationDescription)
        myOperationDescription.Behaviors.Add(serializerBehavior)
    End If

    ' Change settings of the behavior.
    serializerBehavior.MaxItemsInObjectGraph = 10000
    serializerBehavior.IgnoreExtensionDataObject = True

    sh.Open()
    Console.WriteLine("Listening")
    Console.ReadLine()

End Sub 

C#
private void DataContractBehavior()
{
    WSHttpBinding b = new WSHttpBinding(SecurityMode.Message);
    Uri baseAddress = new Uri("http://localhost:1066/calculator");
    ServiceHost sh = new ServiceHost(typeof(Calculator), baseAddress);
    sh.AddServiceEndpoint(typeof(ICalculator), b, "");

    // Find the ContractDescription of the operation to find.
    ContractDescription cd = sh.Description.Endpoints[0].Contract;
    OperationDescription myOperationDescription = cd.Operations.Find("Add");

    // Find the serializer behavior.
    DataContractSerializerOperationBehavior serializerBehavior =
        myOperationDescription.Behaviors.
           Find<DataContractSerializerOperationBehavior>();

    // If the serializer is not found, create one and add it.
    if (serializerBehavior == null)
    {
        serializerBehavior = new DataContractSerializerOperationBehavior(myOperationDescription);
        myOperationDescription.Behaviors.Add(serializerBehavior);
    }

    // Change the settings of the behavior.
    serializerBehavior.MaxItemsInObjectGraph = 10000;
    serializerBehavior.IgnoreExtensionDataObject = true;

    sh.Open();
    Console.WriteLine("Listening");
    Console.ReadLine();
}

System..::.Object
  System.ServiceModel.Description..::.DataContractSerializerOperationBehavior
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
Page view tracker