Method overloading allows a class to have multiple methods with the same name, but, with a different signature. So, in C# methods can be overloaded based on the number, type(int, float etc) and the kind(Value, Ref or Out) of parameters.
WebMethods in a web service can also be overloaded. Notice that the Add() functions below are overloaded based on the number of parameters. The first method adds 2 numbers and the second one adds 3 numbers.
[WebMethod]
public int Add(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
[WebMethod]
public int Add(int firstNumber, int secondNumber, int thirdNumber)
{
return firstNumber + secondNumber + thirdNumber;
}
When we build
the solution, it builds successfully. But when we run the web service
and try to view the service page we get an exception at runtime -
Both
Int32 Add(Int32, Int32, Int32) and Int32 Add(Int32, Int32) use the
message name 'Add'. Use the MessageName property of the WebMethod
custom attribute to specify unique message names for the methods.
To fix this use MessageName property of the WebMethod attribute. MessageName property is used to uniquely identify the individual XML Web service methods.
[WebMethod(MessageName="Add3Numbers")]
public int Add(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
When we now try to view the service page we get a different exception -
Service
'WebServicesDemo.CalculatorWebService' does not conform to WS-I Basic
Profile v1.1. Please examine each of the normative statement violations
below. To turn off conformance check set the ConformanceClaims property
on corresponding WebServiceBinding attribute to WsiClaims.None.
R2304:
Operation name overloading in a wsdl:portType is disallowed by the
Profile. A wsdl:portType in a DESCRIPTION MUST have operations with
distinct values for their name attributes. Note that this requirement
applies only to the wsdl:operations within a given wsdl:portType. A
wsdl:portType may have wsdl:operations with names that are the same as
those found in other wsdl:portTypes.
- Operation 'Add' on portType 'CalculatorWebServiceSoap' from namespace 'http://pragimtech.com/webservices'.
To make service conformant please make sure that all web methods belonging to the same binding have unique names.
To fix this error, change WebServiceBinding attribute on the web service class
FROM - [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
TO - [WebServiceBinding(ConformsTo = WsiProfiles.None)]
When we create a new webservice in Visual Studio 2005 or 2008, the IDE automatically adds the WebServiceBinding attribute on the web service class.
Step 2: The next step is to update the proxy class in the client application, that is in the CalculatorWebApplication.
To achieve this, right click on CalculatorService in Service References folder and select Update Service Reference option.
Step 3: Copy and paste the following HTML in WebForm1.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="font-family: Arial">
<tr>
<td>
<b>First Number</b>
</td>
<td>
<asp:TextBox ID="txtFirstNumber" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Second Number</b>
</td>
<td>
<asp:TextBox ID="txtSecondNumber" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Third Number</b>
</td>
<td>
<asp:TextBox ID="txtThirdNumber" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Result</b>
</td>
<td>
<asp:Label ID="lblResult" runat="server"></asp:Label>
</td>
<td>
<b>Result1</b>
</td>
<td>
<asp:Label ID="lblResult1" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnAdd" runat="server" Text="Add"
OnClick="btnAdd_Click" />
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvCalculations" runat="server">
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code:
namespace CalculatorWebApplication
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
CalculatorService.CalculatorWebServicesSoapClient client =
new CalculatorService.CalculatorWebServicesSoapClient();
int result = client.Add(Convert.ToInt32(txtFirstNumber.Text),
Convert.ToInt32(txtSecondNumber.Text));
lblResult.Text = result.ToString();
int Result2 = client.Add1(Convert.ToInt32(txtFirstNumber.Text), Convert.ToInt32(txtSecondNumber.Text), Convert.ToInt32(txtThirdNumber.Text));
lblResult1.Text = Result2.ToString();
}
}
}
Build the solution and Run the application
o/p:
Possible web services interview question?
Is method overloading possible in web services?
Yes, use MessageName property of WebMethod attribute.
No comments:
Post a Comment