Objective
Yesterday morning to the hospital to do the medical examination, was told to make an appointment, this thought is to queue up, I even video are ready ... The result is back. I went to the new company in the afternoon to find a house, 2, because the company provided a list of buses, since I only in a few places after the shuttle to find, but missing the company near this important address. Finally found a "Riverview room", on the balcony can see the Qiantang River. The price and now than doubled, tired, don't want to find.
A friend let me put the title prefix "Resignation years ago" 4 words took, OK, I admit, I just rely on this to attract a part of the eyeball.
Sixth set WCF DataContract & DataMember (data and DataMember for WCF)
These days write down about that Mex still a little puzzled, in the morning on the StackOverflow on the search for an answer, feeling written very good, here to share. Address: HTTP://STACKOVERFLOW.COM/QUESTIONS/21522493/WHAT-WAS-THE-DIFFERENCE-BETWEEN-WSDL-MEX-ENDPOINT-IN-WCF. Perhaps if you have webservice experience, it will be easier to understand. From the user's point of view, I tried to get rid of endpoint. The definition of MEX, as well as commenting on the Behaviors node, and then accessing the http://localhost:8080/page gave me such a hint:
Or back to the beginning.
One more thing, StackOverflow answers to us with a meaning, about WCF, as he does, because WCF itself has more complex and interesting things to do to develop our practices.
Today, in the sixth episode, these two are used to modify the nature of the entity class that needs to be serialized, and it will also involve knowntype seeing that it is a useful feature.
First, let's implement a EmployeeService, which is primarily used to query employee based on ID, and to insert employee into the database. Here's how to start:
Database section
We created a new table called employee, on the left is the table structure, the right is the content, the ID column does not use self-increment.
Then 2 new stored procedures, one Spgetemployeebyid, one saveemployee
Select * from where Id=@id;
Insert into Values (@id ,@name,@gender,@dateOfBirth)
The rest of the definition is written out.
Service section
1 Public classEmployee2 {3 Private int_id;4 Private string_name;5 Private BOOL_gender;6 PrivateDateTime _dateofbirth;7 8 Public intId9 {Ten Get{return_id;} One Set{ This. _id =value;} A } - PublicString Name - { the Get{return_name;} - Set{ This. _name =value;} - } - Public BOOLGender + { - Get{return_gender;} + Set{ This. _gender =value;} A } at PublicDateTime dateOfBirth - { - Get{return_dateofbirth;} - Set{ This. _dateofbirth =value;} - } -}
View Code
Above is the definition of the employee class, and one might wonder why the additional private variables are defined, C # this humanized language is not just writing get;set OK? At first I had this kind of doubt. But in fact, here is to explain the problem, specifically so write.
Public classEmployeeservice:iemployeeservice { PublicEmployee GetEmployee (intID) {Employee emp=NULL; varConnStr = configurationmanager.connectionstrings["Wcfemployee"]. ConnectionString; using(varconn =NewSqlConnection (CONNSTR)) {Conn. Open (); varcmd =Conn. CreateCommand (); Cmd.commandtype=System.Data.CommandType.StoredProcedure; Cmd.commandtext="Spgetemployeebyid"; Cmd. Parameters.Add (NewSqlParameter ("ID", id)); varReader =cmd. ExecuteReader (); if(!reader. HasRows)returnEMP; EMP=NewEmployee (); while(reader. Read ()) {emp. Id= Convert.ToInt32 (reader["Id"]); Emp. Name= reader["Name"]. ToString (); Emp. Gender= Convert.toboolean (reader["Gender"]); Emp. dateOfBirth= Convert.todatetime (reader["dateOfBirth"]); } } returnEMP; } Public voidSaveemployee (Employee emp) {varConnStr = configurationmanager.connectionstrings["Wcfemployee"]. ConnectionString; using(varconn =NewSqlConnection (CONNSTR)) {Conn. Open (); varcmd =Conn. CreateCommand (); Cmd.commandtype=System.Data.CommandType.StoredProcedure; Cmd.commandtext="Spsaveemployee"; Cmd. Parameters.Add (NewSqlParameter ("ID", EMP. ID)); Cmd. Parameters.Add (NewSqlParameter ("name", EMP. Name)); Cmd. Parameters.Add (NewSqlParameter ("Gender", EMP. Gender)); Cmd. Parameters.Add (NewSqlParameter ("dateOfBirth", EMP. dateOfBirth)); Cmd. ExecuteNonQuery (); } } }
View Code
Above is EmployeeService.cs with the most basic ADO operation.
Build a console program to host this service and run successfully.
Client Calls
Create a new WebForm client, to achieve the following effect, the code is not affixed, are very basic.
Enter the ID in the ID box to query the corresponding information for this ID.
No hints were found.
Enter the information point to save the prompt and save to the database.
After writing, everything runs smoothly. Before we introduce the following, let's start with a few concepts.
- What are serialization and deserialization?
From a WCF perspective, serialization (serialization) is a conversion process that converts an entity class to XML, which in turn is called deserialization (deserialization) by the process of getting an entity class through an XML file.
- What is the use of serialization
Here is a question and answer on StackOverflow, which basically means to store and transmit data. and serialization is required .
If not specifically specified, WCF uses DataContractSerializer to serialize object (the keyword on the title finally appears).
To serialize the employee, we can use the serializableattribute or datacontractattribute feature to modify the class. But looking at the employee class above, we didn't add those two features. That's because, starting with the framework 3.5, if we don't use the DataContract or DataMember features, then WCF's DataContractSerializer automatically serializes all the public properties in the order of the dictionaries. Let's visit http://localhost:8080/?wsdl to see:
Search DataContract
Then enter the value of the following schemalocation in the Address bar HTTP://LOCALHOST:8080/?XSD=XSD2 Enter:
The employee is a complextype, and the following sequence has his four attributes. This is the default generated schema.
It says we can explicitly tag a class that needs to be serialized by giving a class A serializable or a DataContract attribute, let's look at the difference between the two ways.
First look at the serializable marked with:
As we can see, all the underlined private variables are serialized.
And look at the effect with DataContract:
Since we have only marked the DataContract attribute for the class, no fields have been serialized ... (because there is no serialized field, the client cannot get the corresponding property when it calls the class.)
)
In fact, DataContract should be used in conjunction with DataMember. Also, this is the recommended practice for WCF. Let's implement one here.
Before we do that, let's take a look at the properties that the DataMember attribute contains: Links
With these properties, we have the freedom to control the name, order, and so on when they are serialized.
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Runtime.Serialization;5 usingSystem.Text;6 7 namespaceEmployeeService8 {9 [DataContract]Ten Public classEmployee One { A Private int_id; - Private string_name; - Private BOOL_gender; the PrivateDateTime _dateofbirth; - -[DataMember (Order =1)] - Public intId + { - Get{return_id;} + Set{ This. _id =value;} A } at[DataMember (Order =2)] - PublicString Name - { - Get{return_name;} - Set{ This. _name =value;} - } in[DataMember (Order =3)] - Public BOOLGender to { + Get{return_gender;} - Set{ This. _gender =value;} the } *[DataMember (Order =4)] $ PublicDateTime dateOfBirthPanax Notoginseng { - Get{return_dateofbirth;} the Set{ This. _dateofbirth =value;} + } A } the}
View Code
By adding the DataMember attribute, the fields come back, and the sequence of serialization is sorted according to what I have given.
To summarize, use DataContract and DataMember to control the objects we need to serialize.
The following also have knowtypeattribute knowledge point, seems to have a lot of things good to write, or another open a bar ... There are fewer people who look at the article ...
Thank you!
Years ago Resignation-WCF Introduction (6)