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.
- Introduction to Web Service with Example in ASP.Net
- Web Service Method Attribute Properties
- Consuming Web Service in Web Application
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.
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New WebSite" - "C#" - "ASP.NET Empty Web Application" (to avoid adding a master page).
- Provide the website a name such as "MethodOverloadingInWebService" or another as you wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" then you will see the web service templates.
To learn in-depth about web service please refer to my article Introduction to Web Service with Examples because in this article I do not discuss it in detail.
Now after adding the web service template then the Solution Explorer will look 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 GetAdditionOfNumbers(int a, int b)
{
return a + b;
}
[WebMethod]
public int GetAdditionOfNumbers(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 GetAdditionOfNumber(int a, int b)
{
return a + b;
}
[WebMethod(MessageName = "AdditionOfThreeNumber")]
public int GetAdditionOfNumber(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 and have 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 as follows.
In the preceding image, you saw that the method names are the same but each method provides a 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 CustomerService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
public class CustomerService : System.Web.Services.WebService
{
[WebMethod(MessageName = "AdditionOfTwoNumber")]
public int GetAdditionOfNumber(int a, int b)
{
return a + b;
}
[WebMethod(MessageName = "AdditionOfThreeNumber")]
public int GetAdditionOfNumber(int a, int b, int c)
{
return a + b + c;
}
}
Now let us create the UI with the simple web application so we can understand how the method overloading works.
- Right-click on the existing Solution Explorer.
- Then choose Add New Item.
- 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,
Consuming Web Service in Web Application.
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)
{
// Nothing needed in Page_Load for this example
}
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(obj.GetAdditionOfNumber(a, b));
tdthreeout.InnerText = Convert.ToString(obj.GetAdditionOfNumber(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.
Note. 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.