15 abr What Is Data Contract in Wcf
The accessibility levels of the members (internal, private, protected or public) have no influence on the data contract. A service contract defines the processes that are made accessible to the outside world by the service. A service contract is the interface of the WCF service and tells the outside world what the service can do. It can have service level settings, such as . B is the name of the service and the namespace of the service. Create a custom data type named Employee. This type of data must be identified for serialization and deserialization by mentioning with the attributes [DataContract] and [DataMember]. It is now quite clear what a WCF data contract is? And how to use data contracts in our WCF service. A data contract is a formal agreement between a department and a customer that abstractly describes the data to be exchanged. A DataContract is included in System.Runtime.Serialization. It defines the structure of the data that flows from the customer to the service and vice versa, and is therefore called a structural contract. It can be applied to a class, structure, or enumeration.
Data contracts are similar to XML`s XSD. During deserialization, an uninitialized object is first created without calling constructors for the type. Then all members of the data are deserialized. A data contract is a formal agreement between a department and a customer that abstractly describes the data to be exchanged. That is, to communicate, the customer and the service do not need to use the same types, but only the same data contracts. For each parameter or return type, a data contract defines exactly which data should be serialized (converted to XML) to be exchanged. The data contract can be explicit or implied. Simple types like int, string, etc. have an implicit data contract. Custom objects are explicit or complex types for which you must define a data contract with the [DataContract] and [DataMember] attributes. DataContractSerializer is a sealed class and is inherited from the XmlObjectSerializer, which is contained in the System.Runtime.Serialization namespace.
It serializes and deserializes data contracts or data types in XML. By default, Windows Communication Foundation uses datacontractSerializer for serialization and deserialization. The namespace is a string property of DataContract with a default value such as schemas.datacontract.org/2004/07/yourservicenamespace”. You can change the namespace to suit your needs. Ideally, the data contract namespace should be similar to the ServiceContract namespace and set to bindingNamespace of the appropriate endpoint. Another important feature of WCF is data contract version control. It`s like keeping track of changes to a data type or member after the methods are deployed at the client and service level. The WCF contract specifies the service and its operations. WCF has five types of contracts: service contract, operating contract, data contract, message contract, and error contract. For a complete code example of a WCF service that defines a data contract, see the sample standard data contract. In Windows Communication Foundation, the data contract is an agreement between the parties (that is, a service and a client) that describes the type of data exchanged between them. We may control the content (for example.
B payload or message body) of a SOAP message using the data contract. Explicitly setting the namespace attribute can help you version data contracts. The data contracts in the following example are included in WSDL as different properties and the customer can use one or both versions The version control system allows us to manage changes to an existing data structure or data types. As mentioned earlier, the WCF service contract can be mapped to Web Service Description Language (WSDL), otherwise the WCF data contract can be mapped to the XML schema. The data contract is a structural contract, another contract in this category is the news contract. Let`s understand the data contract in WCF by implementing a simple real-world example. In simpler terms, we can say that it is essentially the data that is transmitted or returned by a service method when a service method is called. For example, consider a WCF StudentService service with the SaveStudentInfo service method that uses the StudentInfo object as a parameter. In this case, StudentInfo is our data contract, which is exchanged between the WCF service and the client. It can be easily understood by the diagram below. This type is serializable regardless of whether the type used for the generic type parameter (T) is serializable or not.
Because it must be possible to serialize all data members, the following type is serializable only if the generic type parameter is also serializable, as shown in the following code. IsReference is a Boolean attribute with the default value of WCF DataContract. When the value is set to True, the object reference data is retained so that DataContract can serialize an object diagram that contains circular references. It introduces .Net 3.5 SP1 and defines net 3.0 SP2 Data Contract as a data type (for example. B, StudentInfo) is serialized at one end and then deserialized at the other end. Before an object is transmitted over the network, it is serialized (meaning that the object converts to a sequence of bytes) and deserialized at the other end (which means that the sequence of bytes is converted back into an object). By default, Windows Communication Foundation (WCF) uses a serializer called a data contract serializer to serialize and deserialize data (convert it to XML). .