"Go" WebService creation, deployment and use

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

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingsystem.web; usingSystem.Web.Services; namespaceCalculateservice {/// <summary>      ///Summary description of WebService1/// </summary>  [WebService (Namespace="http://tempuri.org/")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)]      //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line. //[System.Web.Script.Services.ScriptService]     Public classCalCulService:System.Web.Services.WebService {[WebMethod] Public stringHelloWorld () {return "Hello World"; }      }  }  

Remove the Hellowworld code, and modify it as follows

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingsystem.web; usingSystem.Web.Services; namespaceCalculateservice {/// <summary>      ///Summary description of Calculservice/// </summary>[WebService (Namespace ="Http://login.wxjy.info")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)]      //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line. //[System.Web.Script.Services.ScriptService]     Public classCalCulService:System.Web.Services.WebService {[WebMethod] Public intSum (intAintb) {returnA +b; } [WebMethod] Public intSub (intAintb) {returnAb; } [WebMethod] Public DoubleMult (DoubleADoubleb) {returnAb; } [WebMethod] Public DoubleDiv (DoubleADoubleb) {returnAb; }      }  }  

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

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingsystem.web; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; //don't forget to add a using reference for WebServiceusingTestcalcul.mywebservice; namespaceTestcalcul { Public Partial classWebForm1:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) {Calculservice cal=NewCalculservice (); DoubleA =29755; Doubleb =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.

Original address: http://blog.csdn.net/yexuanbaby/article/details/9029605/

"Go" WebService creation, deployment and use

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.