Let's take a look at the fifth data protocol and Message Protocol (with the source code attached) in WCF, and the fifth article in wcf.

Source: Internet
Author: User

Let's take a look at the fifth data protocol and Message Protocol (with the source code attached) in WCF, and the fifth article in wcf.

A: Data agreements ("Data agreements" are formal agreements between services and clients, used to describe the data to be exchanged in an abstract way. That is to say, for communication, the client and service do not have to share the same type, but only need to share the same data protocol. Data protocols precisely define each parameter or return type as the data to be serialized for exchange (which data is converted to XML) from MSDN) that is to say, the data protocol is a data protocol between the client and the server. The parameters that communicate with each other are serialized and then transmitted. Then I use graphs to show why data protocols are used.

The following diagram is used to describe its usefulness.

 

Now let's take a look at the code implementation of the Data Protocol.

1: Create a Protocol Class

1 [DataContract] 2 public class People 3 {4 /// <summary> 5 /// ID 6 /// </summary> 7 [DataMember (Name = "MID ")] 8 public string ID {set; get ;} 9 /// <summary> 10 // Name 11 /// </summary> 12 [DataMember (Name = "MName")] 13 public string Name {set; get;} 14 /// <summary> 15 /// serial number 16 /// </summary> 17 [DataMember (Name = "MNumber")] 18 public string Number {set; get ;} 19 /// <summary> 20 /// Age 21 /// </summary> 22 [DataMember (Name = "Mage")] 23 public string Age {set; get;} 24}Protocol

2: Create an Interface

[OperationContract]
People GetPeople (string ID );

3: Implementation Interface

1 public class Service: IService 2 {3 public People GetPeople (string ID) 4 {5 People pp = new People (); 6 pp. ID = Guid. newGuid (). toString (); 7 pp. name = "Pony"; 8 pp. age = "22"; 9 pp. number = "13559846"; 10 return pp; 11}Implementation Interface

4: Check the effect of the server in the previous article.

5: explanation: the properties of the protocol class are defined as follows to protect the class entity.

The above is a simple data protocol. Please correct the deficiencies

B: Message Protocol

Why message protocols:

You may want to use a single type to represent the entire message. Although data protocols can be used for this purpose, we recommend that you use message protocols to perform this operation, which avoids unnecessary packaging levels in the resulting XML. In addition, you can use message protocols to control more messages. For example, you can decide which segments should be included in the message body and which segments should be included in the message header.

We can see that message protocols are easier to control information than data protocols.

Let's use the code to express it.

1: First, we define a header message (specifically used to detect user login information)

1 [MessageContract] 2 public class CheckInfo 3 {4 /// <summary> 5 /// UserName 6 /// </summary> 7 [MessageHeader] 8 public string UserName {set; get;} 9 // <summary> 10 // password 11 /// </summary> 12 [MessageHeader] 13 public string Pwd {set; get;} 14}Verification class

2: start to define the body information (User Information)

1 [MessageContract] 2 public class UserInfo3 {4 [MessageBodyMember] 5 public string RealName {set; get ;}6 [MessageBodyMember] 7 public string Age {set; get ;} 8 [MessageBodyMember] 9 public Details Udetails {set; get ;}Body class

3: After this is done, we will implement the Code (first, we will verify the code in the header message)

Public UserInfo RetUserInfo (CheckInfo cki) {UserInfo uf = new UserInfo (); if (cki. userName = "admin") {if (cki. pwd = "123456") {Console. writeLine (cki. userName + ": logon successful {0}", DateTime. now); uf = GetUserInfo () ;}} else {Console. writeLine (cki. userName + ": Logon Failed {0}", DateTime. now);} return uf ;}Some source code

4. Start the service client for reference.

Note: The input Message Protocol (parameter) as the operation protocol encapsulates all in parameters of the operation method, and the Message Protocol (return) as the return value of the operation protocol encapsulates the out parameters and return values.

That is to say, the input parameter is of the in type (dismember the input class). For details about the out type, refer to the source code.

5: Effect

Server

Client:

Source code download

So much. Thank you for your correction.

Related Article

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.