Creation, deployment, and use of WebService

Source: Internet
Author: User

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 PlainCopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Web;
  5. Using System.Web.Services;
  6. Namespace Calculateservice
  7. {
  8. // <summary>
  9. /// WebService1 Summary Description
  10. // </summary>
  11. [WebService (Namespace = "http://tempuri.org/")]
  12. [WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
  13. [System.ComponentModel.ToolboxItem (false)]
  14. //To allow the use of ASP. NET AJAX to invoke this Web service from a script, uncomment the following line.
  15. //[System.Web.Script.Services.ScriptService]
  16. public class CalCulService:System.Web.Services.WebService
  17. {
  18. [WebMethod]
  19. public string HelloWorld ()
  20. {
  21. return "Hello World";
  22. }
  23. }
  24. }


Remove the Hellowworld code, and modify it as follows

[CSharp]View PlainCopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Web;
  5. Using System.Web.Services;
  6. Namespace Calculateservice
  7. {
  8. // <summary>
  9. /// Calculservice Summary Description
  10. // </summary>
  11. [WebService (Namespace = "Http://login.wxjy.info")]
  12. [WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
  13. [System.ComponentModel.ToolboxItem (false)]
  14. //To allow the use of ASP. NET AJAX to invoke this Web service from a script, uncomment the following line.
  15. //[System.Web.Script.Services.ScriptService]
  16. public class CalCulService:System.Web.Services.WebService
  17. {
  18. [WebMethod]
  19. public int Sum (int A, int b)
  20. {
  21. return a + B;
  22. }
  23. [WebMethod]
  24. public int Sub (int A, int b)
  25. {
  26. return a-B;
  27. }
  28. [WebMethod]
  29. public Double Mult (double A, double b)
  30. {
  31. return a * b;
  32. }
  33. [WebMethod]
  34. public Double Div (double A, double b)
  35. {
  36. return a/b;
  37. }
  38. }
  39. }


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 PlainCopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Web;
  5. Using System.Web.UI;
  6. Using System.Web.UI.WebControls;
  7. Don't forget to add a using reference for WebService
  8. Using Testcalcul.mywebservice;
  9. Namespace Testcalcul
  10. {
  11. Public partial class WebForm1:System.Web.UI.Page
  12. {
  13. protected void Page_Load (object sender, EventArgs e)
  14. {
  15. Calculservice cal = new Calculservice ();
  16. Double A = 29755;
  17. double b = 112.58;
  18. Response.Write (Cal. Mult (A, B). ToString ());
  19. }
  20. }
  21. }


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

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.