Method Overloading In Web Services

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.
I hope you have read those articles. Now let us start from a definition.

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 Services 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:
public class CustomerService : System.Web.Services.WebService  
{  
  
    [WebMethod]  
    public int GetAddtionOfNumber(int a,int b)  
    {  
          
        return  a + b;  
    }  
    [WebMethod]  
    public int GetAddtionOfNumber(int a, int b,int c)  
    {  
  
        return a + b + c;  
    }   
  
      
}  
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:
public class CustomerService : System.Web.Services.WebService  
{  
  
    [WebMethod(MessageName = "AdditionOfTwoNumber")]  
    public int GetAddtionOfNumber(int a,int b)  
    {  
          
       return a + b;
    }  
    [WebMethod(MessageName = "AdditionOfThreeNumber")]  
    public int GetAddtionOfNumber(int a, int b,int c)  
    {  
  
        return a + b+c;
    }   
  
      
}  
    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:
    [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:
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
    using System.Web.Services;  
      
    /// <summary>  
    /// Summary description for CustmomerService  
    /// </summary>  
    [WebService(Namespace = "http://tempuri.org/")]  
      
    [WebServiceBinding(ConformsTo = WsiProfiles.None)]  
      
    public class CustomerService : System.Web.Services.WebService  
    {  
      
        [WebMethod(MessageName = "AdditionOfTwoNumber")]  
        public int GetAddtionOfNumber(int a,int b)  
        {  
              
            return  a + b;  
        }  
        [WebMethod(MessageName = "AdditionOfThreeNumber")]  
        public int GetAddtionOfNumber(int a, int b,int c)  
        {  
      
            return a + b + c;  
        }   
      
          
    }  
    
    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:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
      
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
      
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head id="Head1" runat="server">  
        <title></title>  
    </head>  
    <body style="background-color:Blue">  
    <h4 style="color:White">Article by Vithal Wadje</h4>  
        <form id="form1" runat="server">  
        <table style="margin-top:60px;color:White">  
        <tr>  
        <td>First Number</td>  
        <td>   
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>  
        </tr>  
           <tr>  
        <td>Second Number</td>  
        <td>   
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>  
        </tr>  
           <tr>  
        <td>Third Number</td>  
        <td>   
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>  
        </tr>  
        <tr>  
        <td></td><td></td>  
        </tr>  
         <tr>  
        <td></td><td>  
            <asp:Button ID="btngetAddition" runat="server" Text="Get Addition"   
                 onclick="btngetAddition_Click" /> </td>  
        </tr>  
        <tr>  
        <td>  
      Addition of Two Num.  
        </td>  
        <td id="tdoutputtwo" runat="server">  
          
        </td>  
        </tr>  
      
          <tr>  
        <td>  
      Addition of Three Num.  
        </td>  
        <td id="tdthreeout" runat="server">  
          
        </td>  
        </tr>  
        </table>  
       
        </form>  
    </body>  
    </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:
    using System;  
      
    public partial class _Default : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
      
        }  
        protected void btngetAddition_Click(object sender, EventArgs e)  
        {  
            int a, b, c;//to store the TextBox Values  
            a =Convert.ToInt32( TextBox1.Text);  
            b = Convert.ToInt32(TextBox2.Text);  
            c=Convert.ToInt32(TextBox3.Text);  
            // creating the object of web service class  
            CustomerService obj = new CustomerService();  
            //assigning web service method return output to the server side HTML td tags   
            tdoutputtwo.InnerText = Convert.ToString(Convert.ToInt32(obj.GetAddtionOfNumber(a,b)));  
            tdthreeout.InnerText = Convert.ToString(Convert.ToInt32(obj.GetAddtionOfNumber(a, b,c)));  
        }  
    }  
    
     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. 
    Download Sample
    MethodOverLoadingInWebService.zip
    Summary
    I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.

    Post a Comment

    www.CodeNirvana.in

    Protected by Copyscape
    Copyright © Compilemode