How To Work With Web Service

In this article I am explaining Web service with an example (Arithmetic Operation).

Web Service

Web service is used to interact with one to another application through HTTP protocols. Web service gets hosted on only http protocol so, Web service does not allow interacting through another protocol as stp and other.

Steps to create a Web service

Step 1: Open Microsoft Visual Studio and go to File, then select new Web site then you will get the following:

empty website

Step 2: Select ASP.NET Empty Web site and change website name as your project, then click  Ok button. Then we will get solution explorer as:

solution explorer

Step 3: Click on website with RM button and select add new item, then select web service and click on Add button

web service

Step 4: Template will appear in Solution Explorer as

Template

Step 5: Write the following code in WebService.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6.   
  7. /// <summary>  
  8. /// Summary description for WebArth  
  9. /// </summary>  
  10. [WebService(Namespace = "http://tempuri.org/")]  
  11. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  12. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
  13. // [System.Web.Script.Services.ScriptService]  
  14. public class WebArth : System.Web.Services.WebService {  
  15.   
  16.     public WebArth () {  
  17.   
  18.         //Uncomment the following line if using designed components   
  19.         //InitializeComponent();   
  20.     }  
  21.   
  22.     [WebMethod]  
  23.     public int Add(int a,int b)  
  24.     {  
  25.         return a+b;  
  26.     }  
  27.     [WebMethod]  
  28.     public int Sub(int a, int b)  
  29.     {  
  30.         return a - b;  
  31.     }  
  32.     [WebMethod]  
  33.     public int Multiply(int a, int b)  
  34.     {  
  35.         return a * b;  
  36.     }  
  37.     [WebMethod]  
  38.     public int Divide(int a, int b)  
  39.     {  
  40.         return a / b;  
  41.     }  
  42.       
  43. }  
This is all about create web service in our project. Now we will discuss about how to consume web service.

Step to Consume Web service

Step 1: Create a new website.

Step 2: Click on Website with RM button and select add new item then select Web form as:

*web form

Then click Add button.

Step 3: Design the Web form as per the requirement, then

Step 4: Add service reference to your web site through the following steps:

 

  1. Click on your Web Service with RM button and select Add service reference, then add service reference window will appear as:

    add reference


  2. Click on Advanced button after that click on add Web References button

  3. Copy the URL from the URL of the browser.

  4. Past the copied URL address in the address textbox of service reference window. Click 'Go' button.

  5. Rename the Web reference name as required and then press Add Reference.

  6. The reference of the Webservice will appear in the Service Reference folder in solution explorer.

  7. Go to Web form page of your project and write the following code.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using sr1;  
  8. public partial class _Default : System.Web.UI.Page  
  9. {  
  10.     WebArth obj = new WebArth();  
  11.     int a, b, c;  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.         
  15.     }  
  16.     protected void TextBox2_TextChanged(object sender, EventArgs e)  
  17.     {  
  18.   
  19.     }  
  20.     protected void Button1_Click(object sender, EventArgs e)  
  21.     {  
  22.         a = Convert.ToInt32(TextBoxfirst.Text);  
  23.         b = Convert.ToInt32(TextBoxsecond.Text);  
  24.         c = obj.Add(a, b);  
  25.         TextBoxResult.Text = Convert.ToString(c);  
  26.     }  
  27.     protected void Button2_Click(object sender, EventArgs e)  
  28.     {  
  29.         a = Convert.ToInt32(TextBoxfirst.Text);  
  30.         b = Convert.ToInt32(TextBoxsecond.Text);  
  31.         c = obj.Sub(a, b);  
  32.         TextBoxResult.Text = Convert.ToString(c);  
  33.     }  
  34.     protected void Button3_Click(object sender, EventArgs e)  
  35.     {  
  36.         a = Convert.ToInt32(TextBoxfirst.Text);  
  37.         b = Convert.ToInt32(TextBoxsecond.Text);  
  38.         c = obj.Multiply(a, b);  
  39.         TextBoxResult.Text = Convert.ToString(c);  
  40.     }  
  41.     protected void Button4_Click(object sender, EventArgs e)  
  42.     {  
  43.         a = Convert.ToInt32(TextBoxfirst.Text);  
  44.         b = Convert.ToInt32(TextBoxsecond.Text);  
  45.         c = obj.Divide(a, b);  
  46.         TextBoxResult.Text = Convert.ToString(c);  
  47.   
  48.     }  
  49. }  
Now run the application and you will get the correct output.

This is all about how to consume or work with Web service.