Requirements for this tutorial
Visual Studio 2008 along with .Net framework 3.5
Steps to create your first web service:
With Visual Studio 2008 installed in your system, creating your first web service is just few clicks away.
1. Open Visual Studio 2008
2. File -> New -> Website
3. Select ASP.NET web service
4. Enter a project name and select “Ok”
Ta da, you should see a set a files which is actually your first web service
Here are a little more details on your new web service.
App_Code\Services.cs is the class file that will contain all your web methods.
Services.asmx is your service file and point of contact for calling the web methods.
In this project I would be creating a simple calculator website which uses the web services for calculation. Now we will rename the files to make sense.
I have renamed Services.cs has GenericCalcService.cs and the class name from Service to GenericCalcService. I have renamed the Services.asmx to Calculator.asmx. As soon as you rename the class and the service files make sure that your service file(asmx) points to the new class name.
After modifications your files will look as follows
GenericCalcService.cs
Calculator.asmx
Let us start building the code behind, i.e. the class file for the web service.
Double click on the GenericCalcService.cs file. By default you should be seeing a default web method HelloWorld(). You can start building a web method as a normal method. To convert any method to a web method, you just have to add the “[WebMethod]” decorative tag above the method’s definition as shown below.
You can add normal methods also into this class file which can be called from other web methods, but these methods will not get exposed outside the class file. Now in order to build this simple calculator, I have created four web methods, each takes two double variables as input and returns the answer as a string variable. Also to convert the double value to string I have added a normal method (d2s) to convert incoming double variable to string.
Also before you compile, change the namespace to a unique url. This URL will be used for calling your web service.
[WebService(Namespace = "http://localhost/calculator/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class GenericCalcService : System.Web.Services.WebService
{
public GenericCalcService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string add(double a, double b)
{
return d2s(a + b);
}
[WebMethod]
public string subtract(double a, double b)
{
return d2s(a – b);
}
[WebMethod]
public string multiply(double a, double b)
{
return d2s(a * b);
}
[WebMethod]
public string divide(double a, double b)
{
return d2s(a / b);
}
public string d2s(double c)
{
return c.ToString();
}
}
Once this is complete right click on this project in the solution explorer and select Build Solution. If there are any errors try and clear the same. Once you have cleared the errors, right click on the asmx page and select “View in Browser”. You should see a web page as shown below.
In this page you can see the available methods. The available methods will not contain the normal methods.
The WSDL Document
Select the “Service Description” link to view WSDL document of your new web service. The WSDL document is a XML document that contains description of the web service and web methods. Each method will contain the request and response tags describing the format of the input and the output. This document will only contain request and response using SOAP1.1, HTTP GET and POST and MIME.
Testing the web service:
For testing the web service, select a method from the service asmx page. This will open a sample default application that can help you to invoke a request and get the response. If you have used the same class and methods I have used above you should see a test page as below. Enter the input values and click on “invoke”. This will invoke a HTTP POST request and display the response from the service.
Built in Test Page:

Result Page:
Congratulations, you have successfully created your first web service using ASP.NET. In the next article I will describe you on how to implement and invoke this web service in real time.





