Data Contract (DataContract)
The service contract defines the remote Access object and the method that is available for invocation, and the data contract is the custom data type to be transferred between the server and the client.
Once a type is declared as DataContract, the type can be serialized between the server and the client, as shown below.
[DataContract]
public class UserInfo
{
....
}
Only objects of the type declared as DataContract can be transmitted, and only member properties are passed, and the member methods are not passed. WCF provides more detailed control over the types declared as DataContract, which can exclude a member from the serialization scope, meaning that the client program does not get any information about the excluded members, including definitions and data. By default, all member properties are excluded, so it is necessary to declare each member to be transferred as DataMember, as shown below.
[DataContract]
public class UserInfo
{
[DataMember]
public string UserName
{
Get
Set
}
[DataMember]
public int Age
{
Get
Set
}
[DataMember]
public string Location
{
Get
Set
}
public string Zodiac
{
Get
Set
}
}
The above code declares the UserInfo class as DataContract, declaring the 3 properties of username, age, and location as DataMember (data members). Zodiac members are not declared as DataMember, so no information about Zodiac is transmitted when the data is exchanged.
DataContract also supports Name/namespace properties, like Servicecontract,name and namespace can customize names and namespaces, The client will use a custom name and namespace to access the DataContract type.
A member declared as DataMember can also customize the client-visible name, for example:
[DataMember (name= "Name")]
public string UserName
{
Get
Set
}
[DataMember (name= "age")]
public int Userage
{
Get
Set
}
In addition to name and namespace, DataMember has the following parameters, which are as follows.
(1) IsRequired: When the value is true, the serialization engine is required to check for the existence of the value of the object, and if none, an exception is thrown.
(2) Order:bool type value, when True, the serialization and deserialization process will be in the order defined by the member, which is extremely important for the deserialization process that relies on the member location.
(3) Emitdefaultvalue: Sets a default value for the member property.
In general, declaring a type as datacontract can satisfy the need for a transfer, but the special case is unavoidable, requiring more precise control over the SOAP message to be transmitted, messagecontract can meet this requirement.
Declaring a type as messagecontract means that it can be serialized as a SOAP message, declaring that the members of the type are parts of the SOAP message, such as header, body, and so on, as shown below.
[MessageContract]
public class Usermessage
{
private string user = String.Empty;
private string authKey = String.Empty;
[Messagebodymember (
Name = "UserName",
Namespace = "http://www.wcf.com")]
public string User
{
get {return user;}
set {user = value;}
}
[MessageHeader (
Name = "AuthKey",
Namespace = "Http://www.wcf.com",
MustUnderstand = True
)]
public string AuthKey
{
get {return authKey;}
set {This.authkey = value;}
}
}
The user member is declared as a member of the MessageBody (message body), and Authkey is declared as a member of the message header (MessageHeader). This class will be able to generate the following SOAP message.
<s:Envelope>
<s:Header>
<a:action s:mustunderstand= "1" >http://UserMessage/Action</a:Action>
</s:Header>
<s:Body>
<usermessage xmlns= "Microsoft.WCF.Documentation" >
<user xmlns= "http://www.wcf.com" >abcd</User>
</UserMessage>
</s:Body>
</s:Envelope>
MessageHeader, the MustUnderstand parameter indicates that the program reading the header must be able to recognize the contents of the header, otherwise it cannot continue processing. The Name/namespace function is the same as the previous element. Another relay parameter, if true, the contents of the header are received and will be sent back to the message sender in the response message.
View: Midnight.
Data Contract (DataContract)