Method Overloading In Web Services

Background

Method overloading is one of the most useful concepts in Object Oriented Programming languages that allow us to avoid the ambiguity of methods by providing uniqueness among them when declaring them. It's common in normal programming to do this but to use this feature in a web service we need to use a certain procedure. So in this article we will learn about Method Overloading in web services from the basics because I have written this article focusing on students and beginners. Before proceeding further please refer to my previous articles for a better understanding of web services.
 
Method Overloading

Method Overloading is the creation of multiple methods in a class with the same name but different parameters and the types are called method overloading. Method overloading is an example of compile time polymorphism that is done at compile time.
Method overloading can be done in a web service with the following things:
  • By changing the number of parameters used.
  • By changing the order of parameters.
  • By using different data types for the parameters
  • The message name property of the Web method attribute must be defined.

Let us demonstrate the preceding points with an example by creating a web service as in the following:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New WebSite" - "C#" - "ASP.NET Empty Web Application" (to avoid adding a master page).
  3. Provide the web site a name such as  "MethodOverloadingInWebService" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer - "Add New Item" then you will see  the web service templates.

To learn in depth about the web service please refer to my article Introduction to Web Service with Examples because in this article I am not disusing it in details.

Now after adding the web service template then the Solution Explorer will look such as follows.
 
 
 
As we have seen in the preceding Solution Explorer, there is the CustomerService.cs  web service class with the CustomerService.asmx file. Now create the two functions having the same name but different parameters as follows:
 
  1. public class CustomerService : System.Web.Services.WebService  
  2. {  
  3.   
  4.     [WebMethod]  
  5.     public int GetAddtionOfNumber(int a,int b)  
  6.     {  
  7.           
  8.         return  a + b;  
  9.     }  
  10.     [WebMethod]  
  11.     public int GetAddtionOfNumber(int a, int b,int c)  
  12.     {  
  13.   
  14.         return a + b + c;  
  15.     }   
  16.   
  17.       

Enabling Method overloading in Web Service


To enable method overloading in a web service use the following procedure.
 
Step 1: Define MessageName property 
 
As I have already said, to enable the method overloading in a web service then we need to use the MessageName property of the Webmethod attribute to make the same named methods unique. If we run the preceding program without the MessageName property then the following error will occur.
 
 
 
So to solve the preceding problem we need to define the MessageName property for the preceding two methods of the CustomerService class. After defining the message name property, the class will look such as follows:
  1. public class CustomerService : System.Web.Services.WebService  
  2. {  
  3.   
  4.     [WebMethod(MessageName = "AdditionOfTwoNumber")]  
  5.     public int GetAddtionOfNumber(int a,int b)  
  6.     {  
  7.           
  8.        return a + b;
  9.     }  
  10.     [WebMethod(MessageName = "AdditionOfThreeNumber")]  
  11.     public int GetAddtionOfNumber(int a, int b,int c)  
  12.     {  
  13.   
  14.         return a + b+c;
  15.     }   
  16.   
  17.       

In the preceding example, I overloaded the GetAddtionOfNumber method and at the client site, to differentiate the two methods, I defined the messageName property to make the two methods unique that has the same name.
 
Step 2: Set WsiProfiles (Web Service Identity) to None
 
By default the WebServiceBinding attribute property of WsiProfiles is BasicProfile1_1 as shown in the following code:
  1. [WebServiceBinding(ConformsTo =WsiProfiles.BasicProfile1_1)] 
If we run the preceding program without setting WsiProfiles to None then the following error will occur:
 
 
To solve the preceding problem we need to set the WsiProfiles.None as in the following:
 
 
 
Now run the Web Service application. Two methods of the web service class will be run without an error and will look such as follows:
 
 
 
In the preceding image you saw that the method names are the same but each method provides different output. Now the entire code of the web service class will look such as follows:
  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 CustmomerService  
  9. /// </summary>  
  10. [WebService(Namespace = "http://tempuri.org/")]  
  11.   
  12. [WebServiceBinding(ConformsTo = WsiProfiles.None)]  
  13.   
  14. public class CustomerService : System.Web.Services.WebService  
  15. {  
  16.   
  17.     [WebMethod(MessageName = "AdditionOfTwoNumber")]  
  18.     public int GetAddtionOfNumber(int a,int b)  
  19.     {  
  20.           
  21.         return  a + b;  
  22.     }  
  23.     [WebMethod(MessageName = "AdditionOfThreeNumber")]  
  24.     public int GetAddtionOfNumber(int a, int b,int c)  
  25.     {  
  26.   
  27.         return a + b + c;  
  28.     }   
  29.   
  30.       

Now let us create the UI with the a simple web application so we can understand how the method overloading works:
  1. Right-click on the existing Solution Explorer.
  2. Then choose Add New Item.
  3. Select the .aspx Page from the template list, define the name and click on OK.
After adding the Default.aspx page into the existing web service the Solution Explorer then will look as follows:
 
 
Now add a button, three TextBoxes, one label and one button to the Default.aspx page body section, then it will look as follows:  
 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head id="Head1" runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body style="background-color:Blue">  
  10. <h4 style="color:White">Article by Vithal Wadje</h4>  
  11.     <form id="form1" runat="server">  
  12.     <table style="margin-top:60px;color:White">  
  13.     <tr>  
  14.     <td>First Number</td>  
  15.     <td>   
  16.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>  
  17.     </tr>  
  18.        <tr>  
  19.     <td>Second Number</td>  
  20.     <td>   
  21.         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>  
  22.     </tr>  
  23.        <tr>  
  24.     <td>Third Number</td>  
  25.     <td>   
  26.         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>  
  27.     </tr>  
  28.     <tr>  
  29.     <td></td><td></td>  
  30.     </tr>  
  31.      <tr>  
  32.     <td></td><td>  
  33.         <asp:Button ID="btngetAddition" runat="server" Text="Get Addition"   
  34.              onclick="btngetAddition_Click" /> </td>  
  35.     </tr>  
  36.     <tr>  
  37.     <td>  
  38.   Addition of Two Num.  
  39.     </td>  
  40.     <td id="tdoutputtwo" runat="server">  
  41.       
  42.     </td>  
  43.     </tr>  
  44.   
  45.       <tr>  
  46.     <td>  
  47.   Addition of Three Num.  
  48.     </td>  
  49.     <td id="tdthreeout" runat="server">  
  50.       
  51.     </td>  
  52.     </tr>  
  53.     </table>  
  54.    
  55.     </form>  
  56. </body>  
  57. </html> 
 
Now to learn how to consume the web service in a web application, please refer to the following article of mine because in this article I am not going to explain it again:
Now, I hope you have read that article. Let us create the object of the web service class CustomerService.cs in the Default.aspx page to access the methods defined in the web service. After creating the object the default.aspx class file code will look as follows:
  1. using System;  
  2.   
  3. public partial class _Default : System.Web.UI.Page  
  4. {  
  5.     protected void Page_Load(object sender, EventArgs e)  
  6.     {  
  7.   
  8.     }  
  9.     protected void btngetAddition_Click(object sender, EventArgs e)  
  10.     {  
  11.         int a, b, c;//to store the TextBox Values  
  12.         a =Convert.ToInt32( TextBox1.Text);  
  13.         b = Convert.ToInt32(TextBox2.Text);  
  14.         c=Convert.ToInt32(TextBox3.Text);  
  15.         // creating the object of web service class  
  16.         CustomerService obj = new CustomerService();  
  17.         //assigning web service method return output to the server side HTML td tags   
  18.         tdoutputtwo.InnerText = Convert.ToString(Convert.ToInt32(obj.GetAddtionOfNumber(a,b)));  
  19.         tdthreeout.InnerText = Convert.ToString(Convert.ToInt32(obj.GetAddtionOfNumber(a, b,c)));  
  20.     }  

 Now run the application, the initial UI will look as follows:
 
 
 
Now enter the input values into the preceding three textboxes and click on the Get Addition button, the output will be as follows:
 
 
 
In the preceding output, the 40 output is returned by the first method that takes two parameters and 60 is the output returned by the second that takes three parameters, hence from the preceding example it's clear that we can overload the methods in web services.
 
 Notes
  • Download the Zip file from the attachment for the full source code of the application.
Summary

I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.
 


Similar Articles