System.Runtime.Serialization 4.0.0.0 System.Attribute System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct | System.AttributeTargets.Enum | System.AttributeTargets.All, AllowMultiple=false, Inherited=false)

Apply the attribute to types (classes, structures, or enumerations) that are used in serialization and deserialization operations by the . If you send or receive messages by using the indigo1 infrastructure, you should also apply the to any classes that hold and manipulate data sent in messages. crabout data contracts, see Using Data Contracts.

You must also apply the to any field, property, or event that holds values you want to serialize. By applying the , you explicitly enable the to serialize and deserialize the data.

You can apply the to private fields. Be aware that the data returned by the field (even if it is private) is serialized and deserialized, and thus can be viewed or intercepted by a malicious user or process.

crabout data contracts, see the topics listed in Using Data Contracts.

## Data Contracts

A data contract is an abstract description of a set of fields with a name and data type for each field. The data contract exists outside of any single implementation to allow services on different platforms to interoperate. As long as the data passed between the services conforms to the same contract, all the services can process the data. This processing is also known as a loosely coupled system. A data contract is also similar to an interface in that the contract specifies how data must be delivered so that it can be processed by an application. For example, the data contract may call for a data type named "Person" that has two text fields, named "FirstName" and "LastName". To create a data contract, apply the to the class and apply the to any fields or properties that must be serialized. When serialized, the data conforms to the data contract that is implicitly built into the type.

A data contract differs significantly from an actual interface in its inheritance behavior. Interfaces are inherited by any derived types. When you apply the to a base class, the derived types do not inherit the attribute or the behavior. However, if a derived type has a data contract, the data members of the base class are serialized. However, you must apply the to new members in a derived class to make them serializable.

## XML Schema Documents and the SvcUtil Tool

If you are exchanging data with other services, you must describe the data contract. For the current version of the , an XML schema can be used to define data contracts. (Other forms of metadata/description could be used for the same purpose.) To create an XML schema from your application, use the Service Model Metadata Utility Tool (Svcutil.exe) with the /dconly command line option. When the input to the tool is an assembly, by default, the tool generates a set of XML schemas that define all the data contract types found in that assembly. Conversely, you can also use the Svcutil.exe tool to create Visual Basic or C# class definitions that conform to the requirements of XML schemas that use constructs that can be expressed by data contracts. In this case, the /dconly command line option is not required.

If the input to the Svcutil.exe tool is an XML schema, by default, the tool creates a set of classes. If you examine those classes, you find that the has been applied. You can use those classes to create a new application to process data that must be exchanged with other services.

You can also run the tool against an endpoint that returns a Web Services Description Language (WSDL) document to automatically generate the code and configuration to create an indigo1 client. The generated code includes types that are marked with the .

## Reusing Existing Types

A data contract has two basic requirements: a stable name and a list of members. The stable name consists of the namespace uniform resource identifier (URI) and the local name of the contract. By default, when you apply the to a class, it uses the class name as the local name and the class's namespace (prefixed with "http://schemas.datacontract.org/2004/07/") as the namespace URI. You can override the defaults by setting the and properties. You can also change the namespace by applying the to the namespace. Use this capability when you have an existing type that processes data exactly as you require but has a different namespace and class name from the data contract. By overriding the default values, you can reuse your existing type and have the serialized data conform to the data contract.

In any code, you can use the word DataContract instead of the longer .

## Versioning

A data contract can also accommodate later versions of itself. That is, when a later version of the contract includes extra data, that data is stored and returned to a sender untouched. To do this, implement the interface.

crabout versioning, see Data Contract Versioning.

Specifies that the type defines or implements a data contract and is serializable by a serializer, such as the . To make their type serializable, type authors must define a data contract for their type.

- Constructor
  4.0.0.0
  To be added.

  Initializes a new instance of the class.

- Property
  4.0.0.0
  System.Boolean To be added.

  Use the IsReference property to instruct the to insert XML constructs that preserve object reference information.

  Gets or sets a value that indicates whether to preserve object reference data.

- Property
  4.0.0.0
  System.String To be added.

  The property is used to give a name to a data contract, which is the name of the type in XML schema. crdefault Data Contract Names.

  By default, the name of a data contract is the name of the type that the is applied to. However, there may be reasons to change this default name. One reason is to allow an existing type to process data that must conform to an existing data contract. For example, there exists a type named Person but the data contract, embodied in an XML schema, requires that the name be Customer. The contract can be satisfied by setting the property value to Customer.

  A second reason is to allow the generation of names that are invalid as type names. For example, if a data contract demands a name that is not allowable as a type name, set the property value to that disallowed name. For instance, the string "$value" is disallowed as a type name but is allowed as a property value.

  Gets or sets the name of the data contract for the type.

- Property
  4.0.0.0
  System.String To be added.

  Use this property to specify a particular namespace if your type must return data that complies with a specific data contract.

  For the data to be successfully transmitted, the name of the data in a data contract must be the same in both the client and the server. Visual Basic projects, by default, add a prefix to the namespace defined in each file (called the “root namespace,” named after the project). Adding this prefix causes the client and server namespaces to be different for the same type. The solution is to set the property to “”, or to explicitly set the data contract namespace in this property.

  Gets or sets the namespace for the data contract for the type.