WebService, the Web service, enables different applications running on different machines to exchange data or integrations without the help of specialized third-party software or hardware.
The first time you select WebService is to replace the database remote connection. We all know that when SQL allows remote, you open a port. But this is not allowed on the servers we have deployed, So I chose WebService instead. Now we can also choose WCF to do it. But this is not the subject of our day.
The data returned by WebService is XML, a data method that we are all familiar with, which greatly facilitates our work. Of course, WebService brings us much more than that. I don't want to speak more. Believe that when you can skillfully use the webservice, You will find that this is a sharp edge of the treasure.
For the first contact with Webservcie, the long theory is not as practical as an example, and what we value more is how we can create a webservice and deploy it, and use it.
Below I will gradually complete a subtraction function of the Webservic.
Development environment: VS2012
IIS environment: WINSERVER2008R2 IIS7.5
First step: Open VS2012, create a new empty project, and note the version of the. NET Framework. Here I choose the . NET Framework 4
After you create a new project, add a WebService item to the project
Open this file and we can see that the code is as follows
[CSharp]View Plaincopyprint?
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Web;
- Using System.Web.Services;
- Namespace Calculateservice
- {
- // <summary>
- /// WebService1 Summary Description
- // </summary>
- [WebService (Namespace = "http://tempuri.org/")]
- [WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
- [System.ComponentModel.ToolboxItem (false)]
- //To allow the use of ASP. NET AJAX to invoke this Web service from a script, uncomment the following line.
- //[System.Web.Script.Services.ScriptService]
- public class CalCulService:System.Web.Services.WebService
- {
- [WebMethod]
- public string HelloWorld ()
- {
- return "Hello World";
- }
- }
- }
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Services; namespace calculateservice{//<summary>//WebService1/// </summary> [ WebService (Namespace = "http://tempuri.org/")] [webservicebinding (ConformsTo = wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)] //To allow the use of ASP. NET AJAX to invoke this Web service from a script, uncomment the following line. //[System.Web.Script.Services.ScriptService] public class CalCulService:System.Web.Services.WebService { [WebMethod] public string HelloWorld () { return ' Hello world '; }} }
Remove the Hellowworld code, and modify it as follows
[CSharp]View Plaincopyprint?
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Web;
- Using System.Web.Services;
- Namespace Calculateservice
- {
- // <summary>
- /// Calculservice Summary Description
- // </summary>
- [WebService (Namespace = "Http://login.wxjy.info")]
- [WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
- [System.ComponentModel.ToolboxItem (false)]
- //To allow the use of ASP. NET AJAX to invoke this Web service from a script, uncomment the following line.
- //[System.Web.Script.Services.ScriptService]
- public class CalCulService:System.Web.Services.WebService
- {
- [WebMethod]
- public int Sum (int A, int b)
- {
- return a + B;
- }
- [WebMethod]
- public int Sub (int A, int b)
- {
- return a-B;
- }
- [WebMethod]
- public Double Mult (double A, double b)
- {
- return a * b;
- }
- [WebMethod]
- public Double Div (double A, double b)
- {
- return a/b;
- }
- }
- }
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Services; namespace calculateservice{//<summary>///Calculservice//</summary> [WebService (Names Pace = "Http://login.wxjy.info")] [webservicebinding (ConformsTo = wsiprofiles.basicprofile1_1)] [system.componentmod El. ToolboxItem (FALSE)]//To allow the use of ASP. NET AJAX to invoke this Web service from a script, uncomment the following line. [System.Web.Script.Services.ScriptService] public class CalCulService:System.Web.Services.WebService { [WebMethod] public int Sum (int a, int b) {return a + B; } [WebMethod] public int Sub (int a, int b) {return a-A; } [WebMethod] public double Mult (double A, double b) {return a * b; } [WebMethod] public double Div (double A, double b) {return A/b; } }}
At this point, we have completed the code section of this webservice. Next we're going to pack this webservice for release.
Right-click on the item and click Publish in the menu
At this point, the following page appears
Click the drop-down menu to select new configuration file
After completing the above steps, we can see the following files generated in the settings file generated folder
Now we're going to copy the resulting file to an already deployed IIS server: Create a new folder on the server, copy the files to the folder, and set anonymous permissions for the folder.
Now we add the Web site in IIS
It is important to note that our WebService is based on. NET 4. If your IIS is using. NET 2.0 by default, an exception will be thrown after publishing and cannot be accessed.
Look at the application pool default settings on the right side of the application pool
Two solutions available: first, in the table above, change the application pool by default to 4.0. Of course, this means you don't have 2.0 of applications and websites in your IIS.
If you have other applications in IIS that require. NET 2.0, you can choose a second option
First add an application pool
Then add the Web site to change the application pool for the site to the new application pool
Of course, we can also take this project. NET version to 3.5, and then repackage the release. Then you don't need to add support for 4.0.
When you are finished, start the Web site and you can close the connection to the server.
Enter the address in the local browser
Http://xxx.xxx.com/calculservice.asmx
At this point we can see the following interface
At this point we have completed the creation and deployment of this webservice. Next, we're going to use it locally
Open VS2012, new project, project named Testcalcul
To add a new item to a project
Right-click on the reference item in the project and select Add Service reference, the display interface is as follows:
Click Advanced in the lower left corner and the interface appears as follows
Click Add Web Reference, the interface appears as follows
Enter the URL of the webservice we published in the URL and the system will automatically search for the service. and set a reference name for the Web reference. This reference will be the namespace of the service that we reference in the project
Click Add Reference to complete the reference.
Next, we write the following code in the CS file of WebForm1
[CSharp]View Plaincopyprint?
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Web;
- Using System.Web.UI;
- Using System.Web.UI.WebControls;
- Don't forget to add a using reference for WebService
- Using Testcalcul.mywebservice;
- Namespace Testcalcul
- {
- Public partial class WebForm1:System.Web.UI.Page
- {
- protected void Page_Load (object sender, EventArgs e)
- {
- Calculservice cal = new Calculservice ();
- Double A = 29755;
- double b = 112.58;
- Response.Write (Cal. Mult (A, B). ToString ());
- }
- }
- }
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using system.web.ui.webcontrols;//don't forget to add a using reference using Testcalcul.mywebservice;namespace for WebService testcalcul{ public partial class WebForm1:System.Web.UI.Page { protected void Page_Load (object sender, EventArgs e) { C4/>calculservice cal = new Calculservice (); Double A = 29755; Double b = 112.58; Response.Write (Cal. Mult (A, B). ToString ());}}}
Next, run the project in the browser. You can see the following results
At this point we have completed the process of using this WebService
In fact, this example does not make any sense in development.
But through this example, we can see the reusability of WebService, simplicity is very good.
In the context of VS, we can easily complete a webservice development process.
The way the XML data is returned to us by WebService can also be fully combined with any language that can handle XML, such as JavaScript.
To fulfill our needs.
It is important to note that when WebService connects to the database, we need to encrypt the webservice appropriately.
There are many ways to encrypt, you can do this by prohibiting anonymous access on the IIS server, or you can add validation to webservice validation, most
The simple thing is to add a user name and password.
Also note, do not use meaningful method names, such as login or Payorder, return data do not use bool value, try to use string to replace
, it is best to add interference codes to the string.
Creation, deployment, and use of WebService