Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 5 - WebMethod overloading in asp.net web services

Suggested Videos
Part 2 - Consuming a web service
Part 3 - Using ASP.NET Session State in a Web Service
Part 4 - WebMethod attribute properties



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. We discussed method overloading in Part 25 of c# tutorial series.



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.
method overloading in web services

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="Add2Numbers")]
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.

Possible web services interview question?
Is method overloading possible in web services?
Yes, use MessageName property of WebMethod attribute.

asp.net webservices tutorial

4 comments:

  1. Hi Venkat Nice Clear cut Explantion,I am follower of your Videos Can You Elaborate On Bindings,Thank you.

    ReplyDelete
  2. Kindly give some tutorials on MVP(Model View Presenter) when You get time it will be useful

    ReplyDelete
  3. Hi Venkat! Your videos are very helpful to me. Even though I am having 5+ years of experience in Windows programming I am not having knowledge on asp.net. Based on your videos, I got clear idea of asp.net and several other courses. I will be very happy if you include AJAX sessions also.

    ReplyDelete
  4. Hermosa explicación, muy Util. Saludos desde Nicaragua.

    ReplyDelete

It would be great if you can help share these free resources