A Preliminary Study on WCF-16: Basic knowledge of WCF data protocols, wcf-16

Source: Internet
Author: User

A Preliminary Study on WCF-16: Basic knowledge of WCF data protocols, wcf-16
Data protocols

 

  • "Data agreement" is a formal agreement between the service and the client, 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 the parameters or return values that need to be serialized during data exchange.

 

Features of data protocols

 

  • By default, WCF uses a serialization engine called a Data Protocol serialization program to serialize and deserialize data (which is converted to XML ). All. NET Framework primitive types (such as integer and string types) and some types that are considered as primitive (such as DateTime and XmlElement) serialization is possible without any additional preparation and is considered to have default data protocols. Considerations when creating a data agreement:

 

  • When creating a basic data contract for a class or structure, you can apply the DataContractAttribute to the class to declare that this type has a data contract. You can apply the DataMemberAttribute Attribute to each Member to define the members (properties, fields, or events) to be serialized ). However, serialization and deserialization are performed even if these attributes are not used for marking. The applicable rules and exceptions are as follows:

 

  • In some applications, it is necessary to know the sending sequence or expected receiving sequence of data in each data member (for example, the display sequence of data in serialized XML ). Sometimes, you must change this order. The basic rules for data sorting include:

 

  • The default data contract name for a given type is the name of this type. To override the default value, set the Name attribute of DataContractAttribute to another Name. The default data member name for a given field or attribute is the name of this field or attribute. To override the default value, set the Name attribute of DataMemberAttribute to another value. The Data Protocol namespace uses a Uniform Resource Identifier (URI. URI can be absolute or relative. By default, a Common Language Runtime Library (CLR) namespace is allocated for a specific type of data protocol. The basic rules for data protocol naming include:

 

  • By default, any given CLR Namespace (in the Clr. Namespace format) will be mapped to the "http://schemas.datacontract.org/2004/07/clr.namespace?namespace. To override this default value, apply the ContractNamespaceAttribute attribute to the entire module or assembly. Alternatively, to control the data protocol namespaces of each type, set the Namespace attribute of DataContractAttribute.

 

Data Protocol example

 

  • The solution is as follows:

  

  • The project structure is described as follows:
Using System. serviceModel; using System. runtime. serialization; namespace Service {[ServiceContract] public interface IUserInfo {[OperationContract] User [] GetInfo ();} [DataContract (Name = "DCUser", Namespace = "http://wangweimutou.DCUser")] public class User {[DataMember (Order = 1)] public int ID {get; set;} [DataMember (Name = "Name", Order = 2)] public string Name {get; set;} [DataMember] private int Age; private string Address; [DataMember] public string Email {get; set ;} [IgnoreDataMember] public string Phone {get; set;} private string _ job; [DataMember] public string Job {get {return _ job;} set {_ job = value ;}} 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 User [] GetInfo () {List <User> listData = new List <User> (); return listData. toArray ();}}}View Code

2. Host: console application and service bearer program. Add a reference to the Service assembly. The code for 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 "> 3. Client: reference the program on the console, start the Host Program boarding service, and enter the following command in the command line to copy the generated UserInfoClient. cs and App. config files to the Client program directory.

  

The UserInfoClient. cs code is as follows:

// -------------------------------------------------------------------------------- // <Auto-generated> // This code is generated by the tool. // Runtime version: 2.0.50727.5485 /// changes to this file may result in incorrect behavior. If // re-generates code, these changes will be lost. // </Auto-generated> // ------------------------------------------------------------------------------ [assembly: System. Runtime. Serialization. ContractNamespaceAttribute (" http://wangweimutou.DCUser ", ClrNamespace =" wangweimutou. dcuser ")] namespace wangweimutou. dcuser {using System. runtime. serialization; [System. diagnostics. debuggerStepThroughAttribute ()] [System. codeDom. compiler. generatedCodeAttribute ("System. runtime. serialization "," 3.0.0.0 ")] [System. runtime. serialization. dataContractAttribute (Name = "DCUser", Namespace =" http://wangweimutou.DCUser ")] Public partial class DCUser: object, System. runtime. serialization. IExtensibleDataObject {private System. runtime. serialization. extensionDataObject extensionDataField; private int AgeField; private string EmailField; private string JobField; private int IDField; private string name Field; public System. runtime. serialization. extensionDataObject ExtensionData {get {return this. extensionDataField;} set {this. extensionDataField = value;} [System. runtime. serialization. dataMemberAttribute ()] public int Age {get {return this. ageField;} set {this. ageField = value;} [System. runtime. serialization. dataMemberAttribute ()] public string Email {get {return this. emailField;} set {this. emailField = value;} [System. runtime. serialization. dataMemberAttribute ()] public string Job {get {return this. jobField;} set {this. jobField = value;} [System. runtime. serialization. dataMemberAttribute (Order = 3)] public int ID {get {return this. IDField;} set {this. IDField = value;} [System. runtime. serialization. dataMemberAttribute (Order = 4)] public string name {get {return this. name Field;} set {this. name Field = value ;}}} [System. codeDom. compiler. generatedCodeAttribute ("System. serviceModel "," 3.0.0.0 ")] [System. serviceModel. serviceContractAttribute (ConfigurationName = "IUserInfo")] public interface IUserInfo {[System. serviceModel. operationContractAttribute (Action =" http://tempuri.org/IUserInfo/GetInfo ", ReplyAction =" http://tempuri.org/IUserInfo/GetInfo Response ")] wangweimutou. dcuser. DCUser [] GetInfo ();} [System. codeDom. compiler. generatedCodeAttribute ("System. serviceModel "," 3.0.0.0 ")] public interface IUserInfoChannel: IUserInfo, System. serviceModel. IClientChannel {} [System. diagnostics. debuggerStepThroughAttribute ()] [System. codeDom. compiler. generatedCodeAttribute ("System. serviceModel "," 3.0.0.0 ")] public partial class UserInfoClient: System. serviceModel. clientBase <IUserInfo>, IUserInfo {public UserInfoClient () {} public UserInfoClient (string endpointConfigurationName): base (endpointConfigurationName) {} public UserInfoClient (string endpointConfigurationName, string remoteAddress): base, remoteAddress) {} public UserInfoClient (string endpointConfigurationName, System. serviceModel. endpointAddress remoteAddress): base (endpointConfigurationName, remoteAddress) {} public UserInfoClient (System. serviceModel. channels. binding binding, System. serviceModel. endpointAddress remoteAddress): base (binding, remoteAddress) {} public wangweimutou. dcuser. DCUser [] GetInfo () {return base. channel. getInfo ();}}View Code

 

Client Data contract Parsing

 

  • The data contract code generated in UserInfoClient. cs is extracted. The code is shown as follows:
[Assembly: System. Runtime. Serialization. ContractNamespaceAttribute (" http://wangweimutou.DCUser ", ClrNamespace =" wangweimutou. dcuser ")] namespace wangweimutou. dcuser {using System. runtime. serialization; [System. diagnostics. debuggerStepThroughAttribute ()] [System. codeDom. compiler. generatedCodeAttribute ("System. runtime. serialization "," 3.0.0.0 ")] [System. runtime. serialization. dataContractAttribute (Name = "DCUser", Namespace =" http://wangweimutou.DCUser ")] Public partial class DCUser: object, System. runtime. serialization. IExtensibleDataObject {private System. runtime. serialization. extensionDataObject extensionDataField; private int AgeField; private string EmailField; private string JobField; private int IDField; private string name Field; public System. runtime. serialization. extensionDataObject ExtensionData {get {return this. extensionDataField;} set {this. extensionDataField = value;} [System. runtime. serialization. dataMemberAttribute ()] public int Age {get {return this. ageField;} set {this. ageField = value;} [System. runtime. serialization. dataMemberAttribute ()] public string Email {get {return this. emailField;} set {this. emailField = value;} [System. runtime. serialization. dataMemberAttribute ()] public string Job {get {return this. jobField;} set {this. jobField = value;} [System. runtime. serialization. dataMemberAttribute (Order = 3)] public int ID {get {return this. IDField;} set {this. IDField = value;} [System. runtime. serialization. dataMemberAttribute (Order = 4)] public string name {get {return this. name Field;} set {this. name Field = value ;}}}}
  • From the code above, we can see that the Name of our data contract has changed to DCUser, Namespace has changed to http://wangweimutou.DCUser, which corresponds to the data contract of the server.
  • The data contract in the proxy class has 6 attributes, and the server data contract defines 8 attributes. The differences are described in the form:

  

Summary

 

  • Through the above example, we understand the serialization sequence and some basic attribute settings of DataContract, as well as some default features of DataContract.

 

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.