add

Saturday, May 23, 2009

Creating a WebService in ASP.NET , C#

This post deals with the basic theory of the web service.

What is a Web Service?

Web Services can convert your applications into Web-applications.

•Web services are application components
•Web services communicate using open protocols
•Web services are self-contained and self-describing
•Web services can be discovered using UDDI
•Web services can be used by other applications
•XML is the basis for Web services

How Does it Work?
The basic Web services platform is XML + HTTP.

XML provides a language which can be used between different platforms and programming languages and still express complex messages and functions.

The HTTP protocol is the most used Internet protocol.

Creating and consuming web service is done in 3 broader steps:

A. Create a Web Service class and method
B. Add Web-Reference to the project
C. Consume the created web service in the ASP.NET code.

A. Create a Web Service class and method

Step 1: Creating Web Project

Open Visual Studio and create a new Web Site. Name it whatever you want. Here, it is named WSDemo.



Step 2: Adding New Item

A new web project will open with a default.aspx and aspx.cs file. Right Click on project and click 'Add New Item'.



This will open the 'Add New Item' Dialoge box. Select template 'Web Service' and name it WS.asmx.



You will notice that two new files has been added: WS.aspx and WS.cs under App_Code



Adding a web service file will add WS.asmx file. If you open the WS.cs file, you will notice lot of pre-coded lines there.



A class must inherit System.Web.Services.WebService in order to behave like a web service.

Also, a HelloWorld method is added by default. The normal method can be converted to a webmethod by adding the following line on top of that method:

[WebMethod]

Step 3: Adding Custom Code:

My objective is to write a webmethod that will accept two parameters and return the sum of those two numbers. So, I removed the HelloWorld method and added my own Add Method.

[WebMethod]
public int Add(inta, int b)
{
return a+b;
}





At this point, your webservice clas is ready.

In order to test whether web service is created successfully or not, build the website and right click the WS.asmx and click 'View in Browser'. This action will open the webservice in browser and display the webmethod as a link.

Clicking on the WebMethod will ask for the input parameters that we defined (a, b) and submitting the form will show the result in a xml file.

B. Add Web-Reference to the project

Now when the web service has been developed, we need to add the web reference to the project. In order to do so , follow these steps:

Step 1: Click Add WebReference

Right click the project and select 'Add Web Reference'



Step 2: Browse for the web service

This action will open the 'Add Web Reference' wizard. It will ask use rto select the location where the web service is their. Since for our purpose, web service is in the solution, click 'Web Service in the solution'



Step 3: Selecting the web service:

Select the web service 'WS'. This will check the webservice 'WS' and list out all the WebMethods. During this process, you will see a progress bar liek this:



Step 4: Defining namespace and adding the reference:

After the internal process, wizard will list out the webmethods found in that webservice. If you chek the image below, there are few things to notice:

i. URL : That is the url fo the webservice. You can access the webservice WS directly from browser also.

ii. Webmethod Name: 'Add' is the name of our web method.

iii. Web Reference Name: This will be the namespace of the webservice.You can change it to your desired name. For demo purpose, I have kept it the default 'localhost'.





Clicking 'Add Reference' will add the web reference to the project and add the following files:



Here 'localhost' is the namespace. There are discovery file and WSDL file. I will explain the file types in next post..

This means web-reference has been added.

C. Consume the created web service in the ASP.NET code.

Now after creating and adding reference, we need to consume the web service.

Step 1: Modify the ASP.NET page:

In the default.aspx, add the following code:



<form id="form1" runat="server">
<div>
Enter value 1:<asp:TextBox ID="A" runat="server"/>
Enter value 2:<asp:TextBox ID="B" runat="server"/>
<asp:Button id="btnSubmit" runat="server" Text="Add" onclick="btnSubmit_Click" />
<hr />
Sub Result: <asp:Label id="lbl" runat="server"/>
</div>
</form>


Now we need to access the webservice from code. That we do by referring the namespace 'localhost'.

In the btnSubit Click event , add the following code:

localhost.WebService AddService=new localhost.WebService();
lbl.Text=AddService.Add(Int32.Parse(A.Text),Int32.Parse(B.Text)).ToString();




Finally, build the website and run the application, default.aspx will open .Enter two values and click 'Add'.
The result will be shown in the label.



Please feel free to contact me if you have any query..

7 comments:

Vikas Garg said...

detailed and nicely writen

Thanks,
Vikas Garg
http://sharepointa2z.blogspot.com

gitolekha said...

Extremely well written with step by step illustration. Looking forward to more.

Gitolekha Ray
http://techolyvia.wordpress.com

RRave said...

Dear Sir,

I have a launched new web site for .NET programming resources. www.codegain.com. I would like to invite to the codegain.com as author and supporter. I hope you will joins with us soon.

Thank You
RRaveen
Founder www.codegain.com

Nilesh Kute said...

Dear Sir ,
It is nicely explained , in detailed.

thanks You

Anonymous said...

thanks a lot buddy..1!

Anonymous said...

please make the following changes:

In the btnSubmit Click event, change

localhost.WebService AddService=new localhost.WebService();

to

localhost.Service AddService=new localhost.Service();

Anonymous said...

Thanks,
iniyarajan