Second, WCF server-side applications
The first step is to create a WCF Service Application project
Open Visual Studio 2015, and on the menu, click File--New project->WCF Service application. Enter "SCF" in the text box for "name" in the pop-up interface. Wcfservice ", then click the" OK "button. Such as.
Step Two, install the entity Framework
1) Use NuGet to download the latest version of the entity Framework 6.1.3. In Solution Explorer-right-click on Project Scf.wcfservice and pop up a menu, select "Manage NuGet packages for solution" and open the NuGet package management interface. Such as.
2) search for entity in the NuGet package management interface, find the latest version of the entity Framework, click Install. Such as.
3) After the installation is complete, such as.
step Three, create the Scf.common project
- On the menu bar, select File-New-to-project, or use the right mouse button in Solution Explorer to pop up the shortcut menu. Such as.
- In the Add New Project dialog box, expand the Visual C # and Windows nodes, and then select the Class Library template.
- In the Name text box, enter Scf.common, and then choose the OK button. Such as.
4. Add a xmlhelper to the Scf.common project that has been created successfully. CS file, write the following code.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Xml.Serialization;usingSystem.IO;namespacescf.common{ Public classXmlhelper {/// <summary> ///deserialization into an object/// </summary> /// <typeparam name= "T" >Object Type</typeparam> /// <param name= "filename" >XML file path</param> /// <returns></returns> Public StaticT parsexml<t> (stringfilename) {T obj=default(T); XmlSerializer Serializer=NewXmlSerializer (typeof(T)); /*If The XML document has a been altered with unknown nodes or attributes, handle them wit H the Unknownnode and Unknownattribute events.*/ //A FileStream is needed to read the XML document.FileStream fs =NewFileStream (filename, filemode.open); Try{obj=(T) serializer. Deserialize (FS); } Catch(System.Exception ex) {strings =Ex. Message; Throwex; } finally{fs. Close (); } returnobj; } /// <summary> ///deserialization into an object/// </summary> /// <param name= "filename" >XML file path</param> /// <param name= "type" >Object Type</param> /// <returns></returns> Public Static ObjectToobject (stringFilename,type Type) { Objectobj; XmlSerializer Serializer=NewXmlSerializer (type); FileStream FS=NewFileStream (filename, filemode.open); Try{obj=Serializer. Deserialize (FS); } Catch(System.Exception ex) {strings =Ex. Message; Throwex; } finally{fs. Close (); } returnobj; } /// <summary> ///deserialization into an object/// </summary> /// <typeparam name= "T" >Object Type</typeparam> /// <param name= "Data" >XML Data Object string</param> /// <returns></returns> Public StaticT deserializer<t> (stringdata) {T obj=default(T); XmlSerializer Serializer=NewXmlSerializer (typeof(T)); Try { using(StringReader sr =NewStringReader (data)) {XmlSerializer XZ=NewXmlSerializer (typeof(T)); Obj=(T) serializer. Deserialize (SR); } } Catch(System.Exception ex) {strings =Ex. Message; Throwex; } returnobj; } /// <summary> ///Creating an XML file/// </summary> /// <param name= "Fullfilename" >XML file name</param> /// <param name= "Data" >XML string</param> Public Static voidCreatexml (stringFullfilename,stringdata) { using(StreamWriter SW =NewStreamWriter (Fullfilename,false, Encoding.UTF8)) {SW. Write (data); } } /// <summary> ///convert an object to a string/// </summary> /// <typeparam name= "T" >Object Type</typeparam> /// <param name= "T" >Object Entity</param> /// <returns></returns> Public Static stringToxml<t>(T t) {using(StringWriter SW =NewStringWriter ()) {XmlSerializer XZ=NewXmlSerializer (T.gettype ()); Xz. Serialize (SW, T); returnSW. ToString (); } } }}
Fourth Step, create the Entity Data Model
- On the menu bar, choose File, New, project.
- In the New Project dialog box, expand the Visual C # and WINODWS nodes, and then select the Class Library template, and in the Name dialog box, enter SCF. Model ", then click the OK button. Create a Scf.model project. Such as.
3. Install the relevant components through NuGet, and see the first step for specific steps. such as the components in the yellow box.
4. on the menu bar, select Project, add New item.
5. In the Add New Item dialog box, select the data node, and then select the ADO Entity Data Model item.
6. In the Name text box, enter Bookmodel, and then choose the Add button. Such as.
7. in the Entity Data Model Wizard, on the Select Model content page, select the EF designer from the database, and then select the Next button. Such as.
8. On the Select your data connection page, perform one of the following steps:
- If the drop-down list contains a data connection to the Test sample database, select the connection.
- or select the New Connection button to configure the new data connection. Such as.
9. Select the corresponding database server in the Connection Properties dialog box, enter a user name and password, and select the database to use. Such as.
10. If your database requires a password, select the Yes, include sensitive data in connection string option button, and then choose the Next button. You can look at the data connection in the figure below.
11. On the Select your version page, select the Entity Frame 6.x option button, and then choose the Next button.
Description |
If you have installed the latest version of Entity Framework 6 with a WCF service in the second step and installed the WCF Data Services Entity Framework Provider package through NuGet, this dialog box will not appear, directly Go to the next step. |
12. On the Select Database Objects page, expand the Tables node, select the Books check box, and then select the Finish button.
The entity Model diagram is displayed, and the bookmodel.edmx file is added to the project.
13. The resulting results are as follows.
WCF Learning Journey-WCF Second Example (v)