Tuesday, 27 March 2012

Creating a WebService in ASP.NET

What is a Web Service?
Web Services can convert your applications into Web-applications.

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.


 Step 2: Adding 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 and multiplication of those two numbers. So, I removed the HelloWorld method and added my own Add and mul method as shown below.

<WebMethod()> _
Public Function Add(ByVal a As Integer, ByVal b As Integer) As Integer

        Return (a + b)

    End Function


    <WebMethod()> _
Public Function Mul(ByVal a As Integer, ByVal b As Integer) As Integer

        Return (a * b)

    End Function

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:





<body>
    <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" />
&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" Text="Mul" />
<hr />
Sub Result: <asp:Label id="lbl" runat="server"/>
</div>
</form>
</body>





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

In the btnSubit and mul Click event , add the following code:

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim s1 As New localhost.WebService()
        lbl.Text = s1.Add(Int32.Parse(A.Text), Int32.Parse(B.Text)).ToString()
    End Sub

    Protected Sub mul_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s1 As New localhost.WebService()
        lbl.Text = s1.Mul(Int32.Parse(A.Text), Int32.Parse(B.Text)).ToString()
    End Sub


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



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

No comments:

Post a Comment