Use C # to make a simplest web service

xiaoxiao2021-03-06  116

Below, let's see how to build and deploy a simplest web service.

Establish web service

1. Create a directory called WebService in the wwwroot directory.

2. Establish the following file:

<% @ Webservice Language = "C #" class = "addnumbers"%>

Using system;

Using system.Web.services;

Public Class AddNumBers: WebService

{

[WebMethod]

Public Int Add (int A, int b) {

Int sum;

SUM = a b;

Return SUM;

}

}

3. Save this file as addservice.asmx (asmx is an extension), saved to WebService's directory

4. Now we have established a web service, which is ready to use

5. Now, you can access this Web service with the URL below:

http: // ip address /webservice/addservice.asmx/add?a=10&b=5

The result will be returned in XML format.

Deploy this service on the client

1. Enter: in the command line:

WSDL http: // ip address / Webservice/mathService.asmx / N: Namesp /out:filename.cs

This action will create a file called FileName.cs

Note: WSDL refers to WebServices Description Language, this program is in the program files / microsoft.net / frameworksdk / bin directory.

Namesp is the name of the namespace we set, which will be used in the implementation code of the client's client's client.

2. Compile

CSC / T: library /r:system.web.dll /r:System.xml.dll filename.cs

The above command will generate a DLL file, the name is the name of the public class in the ASMX file above, in our example, is: addnumbers.dll

3. Place the generated DLL file in the wwwroot / bin directory of the deployment machine.

Call this web service in the ASP / ASPX of the deployment machine

<% @ import namespace = "namesp"%>