Sunday, August 14, 2011

Method Overloading in Web Services


By default Web Services do not support ‘Method Overloading’.

 Reason:
When data is passed to an XML Web service it is sent in a request and when it is returned it is sent in a response. Therefore, if an XML Web service contains two or more XML Web service methods with the same name, no uniquely identification will be there. Hence it will produce error
To implement method overloading in webservices first you must set the MessageNameattribute of your webservice then set your webservice binding ConformsTo attribute toWsiProfiles.None and that’s it! Note that you can do second step in your Web.Config file for all webservices.
Below is an exaple to implement, Overloading in web service.
Modify the attribute as:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
          
   Complete Code:

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

    [WebMethod]
    public string HelloWorld()
    {      
        return "hello";
    }

    [WebMethod(MessageName = "HelloWorld2")]
    public string HelloWorld(int i)
    {
        return "Overloading";
    }   
}

Related Posts Plugin for WordPress, Blogger...