It is not allowed to have overloaded web methods. Trying to do so will not result in compile error but will generate a runtime error saying something like “Error: two methods having name”.
A possible workaround for this situation is to use the MessageName Property for the webmethod Attribute.
Example:
The following Code will cause errors
*********************************************
[WebMethod]
public string GetStatus(string username)
{
return “status1”;
}
[WebMethod]
public string GetStatus(int userId)
{
return “status1”;
}
***************************************************
A possible solution could be :
*********************************************
[WebMethod(MessageName=”GetStatusByName”)]
public string GetStatus(string username)
{
return “status1”;
}
[WebMethod(MessageName=”GetStatusById”)]
public string GetStatus(int userId)
{
return “status1”;
}
***************************************************
Leave a Reply