Next, let's take a look at how to establish and deploy a simple Web service.
Create a Web Service
1. Create a Webservice directory under the wwwroot directory.
2. Create 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 the extension) and save it to the Webservice directory.
4. Now we have created a Web service and are ready for use by the client.
5. Now, you can use the following URL to access this Web Service:
Http: // ip address/Webservice/Addservice. asmx/Add? A = 10 & B = 5
The result is returned in XML format.
Deploy this service on the client
1. Enter the following in the command line:
WSDL http: // ip address/WebService/MathService. asmx/n: NameSp/out: FileName. cs
This operation creates a file called FileName. cs.
Description: WSDL refers to WebServices Description Language. This Program is in the Program FilesMicrosoft. NETFrameworkSDKBin directory.
NameSp is the name of the namespace we set and will be used in the implementation code of the client that deploys this service later.
2. Compile
CSC/t: library/r: system. web. dll/r: system. xml. dll FileName. cs
The above command will generate a dll file named the name of the public class in the above asmx file. In our example, it is: AddNumbers. dll
3. Place the generated dll file in the wwwrootin directory of the deployment machine.
Call this Web service in asp/aspx of the deployment Machine
<% @ Import Namespace = "NameSp" %>
<Script language = "c #" runat = "server">
Public void Page_Load (object o, EventArgs e ){
Int x = 10;
Int y = 5;
Int sum;
// Instantiating the public class of the webservice
AddNumbers AN = new AddNumbers ();
Sum = AN. Add (x, y );
String str = sum. ToString ();
Response. writeline (str );
}
</Script>