How to use overloaded methods with ASP.NET Web Services

In .NET world it’s very common to write methods with same name but different parameters. This is called overloading. And if you use this technique wisely you can prevent some code repetation.

But in the web services world of .net its not allowed by default. If you try you get an exception says : “Bla, bla, bla. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.”

Server Error in ‘/ICFWebServices’ Application.
Both ICFWebServices.Entities.Portal GetCategorizedRoomData(System.String) and ICFWebServices.Entities.Portal GetCategorizedRoomData() use the message name ‘GetCategorizedRoomData’.  Use the MessageName property of
the WebMethod custom attribute to specify unique message names for the methods.
 

First you must differ your methods with WebMethod’s properties.

[WebMethod(MessageName = "GetCategorizedRoomData",
     Description = "Gets the room list for configured portal name")]
public Portal GetCategorizedRoomData()
{
    string portal = "ICanFootball"; //Buraya web.configden portal okuma gelecek
    return populateCategorizedRoomData(portal);
}

[WebMethod(MessageName = "GetCategorizedRoomDataByPortalName",
     Description = "Gets the room list for given portal name")]
public Portal GetCategorizedRoomData(string Portal)
{
    return populateCategorizedRoomData(Portal);
}

 Second you have to change your web service class Wsi profile to none

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
public class RoomService : System.Web.Services.WebService
{

If you forget step 2 you get an exception like

Server Error in ‘/ICFWebServices’ Application.
Service ‘ICFWebServices.RoomService’ 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 ‘GetCategorizedRoomData’ on portType ‘RoomServiceSoap’ from namespace ‘http://tempuri.org/’.
To make service conformant please make sure that all web methods belonging to the same binding have unique names.

Happy coding.

Posts created 141

Leave a Reply

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top