We study WCF fifth data contract and message contract (attach source code)

Source: Internet
Author: User

A: Data Contract (a "Data contract" is a formal agreement between a service and a client that describes the data to be exchanged in an abstract manner. That is , in order to communicate, clients and services do not have to share the same type, but simply share the same data contract . data contracts precisely define for each parameter or return type what data is serialized for interchange (which data is converted to XML) from MSDN It also says that the data contract is a data protocol between the client and the server., the parameters that communicate with each other are serialized and then transmitted. So I'm using a graph to show why I'm using data contracts.

Here are some of the uses illustrated below.

So let's take a look at the code implementation of the data contract

1: Create a contract class

1 [DataContract]2     Public classpeople3     {4         /// <summary>5         ///ID6         /// </summary>7[DataMember (Name="MID")]8          Public stringID {Set;Get; }9         /// <summary>Ten         ///name One         /// </summary> A[DataMember (Name="Mname")] -          Public stringName {Set;Get; } -         /// <summary> the         ///Serial Number -         /// </summary> -[DataMember (Name="Mnumber")] -          Public stringNumber {Set;Get; } +         /// <summary> -         ///Age +         /// </summary> A[DataMember (Name="Mage")] at          Public stringAge {Set;Get; } -}
Contract Class

2: Create an interface

People GetPeople (string ID);

3: Implement Interface

1   Public classService:iservice2     {3         PublicPeople GetPeople (stringID)4        {5People pp=Newpeople ();6Pp.id =Guid.NewGuid (). ToString ();7Pp. Name ="Little Pony";8Pp. Age =" A";9Pp. Number ="13559846";Ten            returnpp; One}
implementing Interfaces

4: The service end of the same article to see the effect

5: Explanation: Our properties in the contract class are finally followed by what we define to protect the class entities.

These are the data contracts that I simply understand. Please correct me for the shortcomings.

B: Message Contract

Why use a message contract:

You may want to use a single type to represent the entire message. Although data contracts can be used for this purpose, it is recommended that you use message contracts to do this, which avoids unnecessary wrapper levels in the resulting XML. In addition, the use of message contracts allows for more control over the resulting messages. For example, you can decide which segments of information should be included in the message body and which segments should be included in the message header

We can see that the message contract has more control over the information than the data contract.

So let's use the code to show

1: First we define a header message (specifically for detecting user login information)

1 [MessageContract]2     Public classCheckinfo3     {4         /// <summary>5         ///User name6         /// </summary>7 [MessageHeader]8          Public stringUserName {Set;Get; }9         /// <summary>Ten         ///Password One         /// </summary> A [MessageHeader] -          Public stringPWD {Set;Get; }  -}
Validation Class

2: Start defining body information (user information)

1 [MessageContract]2     Public classUserInfo3     {4 [Messagebodymember]5          Public stringRealname {Set;Get; }6 [Messagebodymember]7          Public stringAge {Set;Get; }8 [Messagebodymember]9          PublicDetails Udetails {Set;Get; }
Body Class

3: After doing this we will implement the code (first in the header message to verify)

   PublicUserInfo retuserinfo (checkinfo cki) {UserInfo uf=NewUserInfo (); if(Cki. UserName = ="Admin")           {               if(Cki. PWD = ="123456") {Console.WriteLine (Cki. UserName+": Login succeeded {0}", DateTime.Now); UF=GetUserInfo (); }           }           Else{Console.WriteLine (cki). UserName+": Login failed {0}", DateTime.Now); }           returnuf; }
part of the source

4: Start the service client for reference

Note: The input message contract (parameter) that acts as the contract encapsulates all the in parameters of the action method, and the message contract (return) that is the return value of the operation contract encapsulates the out parameter and the return value.

That is, the input parameter is in the type (the input class is dismembered) returned when the type specific reference source

5: Effect

Service side


SOURCE download

All right, that's all. Please correct me.

We study WCF fifth data contract and message contract (attached source code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.