A Preliminary Study on WCF-17: the equivalence of WCF data protocols, a preliminary study on wcf-17 protocols

Source: Internet
Author: User

A Preliminary Study on WCF-17: the equivalence of WCF data protocols, a preliminary study on wcf-17 protocols
Data contract equivalenceFeatures

 

  • If the client successfully sends data of a certain type to the service or the service successfully sends the data to the client, the receiving end does not necessarily have to send data of this type. The only requirement is that the two types of data protocols should be equivalent.
  • To make the data protocol equivalent, the namespace and name must be the same. In addition, each data member on one end must have an equivalent data member on the other end. Note: The data protocol name, namespace, and data member name are case sensitive.
  • To make the data member equivalent, the name must be the same. In addition, they must also represent the same type of data, that is, their data protocols must be equivalent.
  • If the same end (sender or receiver) has two types, and their data protocols are not equivalent (for example, their data members are different ), you should not specify the same name and namespace for them. Otherwise, an exception may occur.
  • Using the Order attribute of the DataMemberAttribute class may affect the equivalence of data protocols. Its members must appear in the same order, and data protocols can be equivalent.

 

Description of Data contract equivalence

 

   

  • In, we define data protocols as User and Person classes on the server. they generate the same client data contract type as marked 3 on the client. Therefore, User and Person are equivalent data contracts.
  • The equivalence requirements of data protocols are only related to the above overview of the equivalence characteristics of data protocols, and are not related to the category definitions of data protocols themselves. That is to say, as long as the data contract type defined by the server generates the same data contract in the client proxy class, we will say that the data contract of the server is equivalent.
  • With the above features, we can replace the equivalent data protocol on the server without having to modify or regenerate the client, and the client can run normally.

 

Data contract equivalence example

 

  • The solution is as follows:

  

  • Engineering Structure Description
Using System. serviceModel; using System. runtime. serialization; namespace Service {[ServiceContract] public interface IUserInfo {[OperationContract] User [] GetInfo ();} [DataContract] public class User {[DataMember] public int ID {get; set;} [DataMember] public string Name {get; set;} [DataMember] public int Age {get; set;} [DataMember] public string Nationality {get; set ;}}}View Code

The UserInfo. cs code is as follows:

Using System; using System. collections. generic; using System. linq; using System. text; namespace Service {public class UserInfo: IUserInfo {public User [] GetInfo () {List <User> Users = new List <User> (); Users. add (new User {ID = 1, Name = "JACK", Age = 20, Nationality = "CHINA"}); Users. add (new User {ID = 2, Name = "TOM", Age = 18, Nationality = "JAPAN"}); Users. add (new User {ID = 3, Name = "SMITH", Age = 22, Nationality = "KOREA"}); Users. add (new User {ID = 4, Name = "ALENCE", Age = 21, Nationality = "INDIA"}); Users. add (new User {ID = 5, Name = "JOHN", Age = 22, Nationality = "SINGAPORE"}); return Users. toArray ();}}}View Code

2. Host: the console application and the WCF Service bearer program. Add a reference to the Service assembly. After completing the following code and configuration, You can host the Service. The Code of Program. cs is as follows:

Using System; using System. serviceModel; using Service; namespace Host {class Program {static void Main (string [] args) {using (ServiceHost host = new ServiceHost (typeof (UserInfo) {host. opened + = delegate {Console. writeLine ("the service has been started. Press any key to terminate! ") ;}; Host. Open (); Console. Read ();}}}}View Code

The code for App. config is as follows:

<? Xml version = "1.0"?> <Configuration> <system. serviceModel> <services> <service name = "Service. userInfo "behaviorConfiguration =" mexBehavior "> The svcutil.exe tool is used to generate the client proxy class and client configuration file.

Svcutil.exe is a command line tool located in the path C: \ Program Files (x86) \ Microsoft SDKs \ Windows \ v7.0A \ Bin. You can run the command line tool to generate a client proxy class.

  • Run cmd to open the command line and enter cd C: \ Program Files (x86) \ Microsoft SDKs \ Windows \ v7.0A \ Bin
  • Input svcutil.exe/out: f: \ UserInfoClient. cs/config: f: \ App. config http: // localhost: 1234/UserInfo

3. Client: console application and Client calling program. Copy the generated UserInfoClient. cs and App. config to the Client project directory to complete the Client call code. The Code of Program. cs is as follows:

Using System; using System. collections. generic; using System. linq; using System. text; using Service; namespace Client {class Program {static void Main (string [] args) {UserInfoClient proxy = new UserInfoClient (); User [] Users = proxy. getInfo (); Console. writeLine ("{0,-10} {1,-10} {2,-10} {3,-10}", "ID", "Name ", "Age", "Nationality"); for (int I = 0; I <Users. length; I ++) {Console. writeLine ("{0,-10} {1,-10} {2,-10} {3,-10}", Users [I]. ID. toString (), Users [I]. name. toString (), Users [I]. age. toString (), Users [I]. nationality. toString ();} Console. read ();}}}View Code

4. The program running effect is as follows:

  

 

Data Protocol equivalence Verification

 

  • Next, we modify the data protocol of the server and the implementation code of the operation protocol. The IUserInfo. cs code is as follows:
Using System. serviceModel; using System. runtime. serialization; namespace Service {[ServiceContract] public interface IUserInfo {[OperationContract] Person [] GetInfo ();} [DataContract (Name = "User")] public class Person {[DataMember] public int ID {get; set;} [DataMember (Name = "Name")] public string Name {get; set ;} [DataMember] public int Age {get; set;} [DataMember] public string Nationality {get; set ;}}}

The code for UserInfo. cs is as follows:

Using System; using System. collections. generic; using System. linq; using System. text; namespace Service {public class UserInfo: IUserInfo {public Person [] GetInfo () {List <Person> Persons = new List <Person> (); Persons. add (new Person {ID = 1, name = "JACK", Age = 20, Nationality = "CHINA"}); Persons. add (new Person {ID = 2, name = "TOM", Age = 18, Nationality = "JAPAN"}); Persons. add (new Person {ID = 3, name = "SMITH", Age = 22, Nationality = "KOREA"}); Persons. add (new Person {ID = 4, name = "ALENCE", Age = 21, Nationality = "INDIA"}); Persons. add (new Person {ID = 5, name = "JOHN", Age = 22, Nationality = "SINGAPORE"}); return Persons. toArray ();}}}
  • After modification, save and regenerate the Host Program. The Client program does not perform any operation. The running result is the same as the previous one, indicating that the two data protocols are equivalent. The program running effect is as follows:

  

  • Next, we modify the data protocol again, add the Order attribute to the members of the Data Protocol, modify the code as follows, and then re-compile the Host to run the program. After running the program, we found that no error was reported. However, the value of ID and Age is changed to 0, indicating that the equivalence of the contract is affected by Order.
[DataContract (Name = "User")] public class Person {[DataMember (Order = 2)] public int ID {get; set ;} [DataMember (Name = "Name")] public string Name {get; set;} [DataMember (Order = 3)] public int Age {get; set ;} [DataMember (Order = 0)] public string Nationality {get; set ;}}

 

  

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.