This article we will explain how to create a web service in ASP.NET.
What is Web Service
A "web service is the communication platform between two different or same platform applications that allows to use their web method."
Once a web service is created and hosted on the server in the internet it can be consumed by any kind of application developed in any technology.
In the following figure select “Templates”,
Visual C#, then ASP.NET Web Application, and here I give the name of project “webservice”. You can
give the project name as you wish.
And then click on “OK” button
Now right click on project name “webservice” to add webservice like as following figure.
Choose the Web Service template and Enter the name (for example: MyWebService.asmx) then click on "Add".
This will create the following two files
Open the file MyWebService.asmx.cs and write the following code
/// Summary description for MyWebService
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyWebService : System.Web.Services.WebService
public string HelloWorld()
return "Hello World";
public int Add(int x, int y)
return x + y;
public int Sub(int x, int y)
return x - y;
public int Mul(int x, int y)
return x * y;
public int Div(int x, int y)
return x / y;
Attaching the WebMethod attribute to a Public method indicates that you want the method exposed as part of the XML Web service. You can also use the properties of this attribute to further configure the behavior of the XML Web service method. The WebMethod attribute provides the following properties
- 1. BufferResponse
- 2. CacheDuration
- 3. Description
- 4. EnableSession
- 5. MessageName
- 6. TransactionOption
To see whether the service is running correctly go to the
Solution Explorer then open " MyWebService.asmx "
and run your application.
Now you will find all the method names in the browser. And click to add method name.
Now you will see two TextBoxes for checking. Enter the value for x and y and click on the "Invoke" button.
Now you will see the result in an open standard form (XML).
I hope it will help you after reading.