Introduction
Tools Used: .net SDK
How to use: This article explains how to create and deploy a simple webservice .
To use the sample code, one needs to have .net SDK installed. A text editor is enough to build and test the codes.
Creating a Simple Web Service
- Create a folder named Webservice under wwwroot.
- Create a 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;
}
}
- Save this file as AddService.asmx [asmx-> file extension]
- Now the webservice is created and ready for the clients to use it.
- Now we can call this webservice using http://ip address/Webservice/Addservice.asmx/Add?a=10&b=5
This will return the result in XML format.
Deploying the Web Service on the Client Machine
- At the command prompt:
WSDL http://ip address ofthe site/WebService/MathService.asmx /n:NameSp /out:FileName.cs
This will create a file called FileNmame.cs.
WSDL -> WebServices Description Language (This is an application available at C:\Program Files\Microsoft.NET\FrameworkSDK\Bin)
NameSp -> Name of the NameSpace which will be used in client code for deploying the webservice.
- Compilation
CSC /t:library /r:system.web.dll /r:system.xml.dll CreatedFile.cs
This will create a dll with the name of the public class of the asmx file.(In our case, it is "AddNumbers.dll").
CSC is an application available at C:\WINNT\Microsoft.NET\Framework\v1.0.2914
- Put the dll file inside WWWRooT\BIN [Create a BIN Folder in WWWRoot].
Making use of WebService in client asp/aspx page
<%@ 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>
Note: It is advisable to
- Copy the .asmx file to the folder containing WSDL aplication (C:\Program Files\Microsoft.NET\FrameworkSDK\Bin) before creating cs file.
- Copy the created .cs file to the folder containing CSC application (C:\WINNT\Microsoft.NET\Framework\v1.0.2914) before compliling it.