Quantcast
Channel: DotNetCrack - Web Services
Viewing all articles
Browse latest Browse all 5

Method Overloading in Web Services

$
0
0

By default Web Services do not support ‘Method Overloading’. The reason behind is 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.

Solution: To resolve this, MessageName property is required to be attached to Web Method - To uniquely identify polymorphic methods.

public class Calculator : WebService
{
    [WebMethod]
    public int Add(int i, int j) {
       return i + j;
    }   

    [WebMethod(MessageName="Add2")]
    public int Add(int i, int j, int k) {
       return i + j + k;
    }
}

Reason for the Above Error:
The Overloading is supported by web services. But when WSDL (Web Service Description Language) is generated, it will not be able to make the difference between methods because WSDL does not deal on the base of parameters. By passing web methods –‘MessageName Property’, it changes the method name in WSDL. See the WSDL given below, the operation name is Add but the input method name is AddInt as well as output method name is also same (AddInt). The same will apply for Float also.

<.wsdl:operation name="Add".
            <.wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/".>Add two integer Value<./wsdl:documentation.
            <.wsdl:input name="AddInt" message="tns:AddIntSoapIn" /.
            <.wsdl:output name="AddInt" message="tns:AddIntSoapOut" /.>
<./wsdl:operation.>
<.wsdl:operation name="Add".
            <.wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/".>Add two Float Value<./wsdl:documentation.
            <.wsdl:input name="AddFloat" message="tns:AddFloatSoapIn" /.
            <.wsdl:output name="AddFloat" message="tns:AddFloatSoapOut" /.>
<./wsdl:operation.>

Method Overloading in .Net 1.x vs .Net 2.0:
This works perfectly well in .Net 1.x. But in .Net 2.0 this solution solves only one problem. The other problem is related to the ‘Conformity to WS-I Basic Profile v1.1’. In .Net 2.0, by default below two attributes are added to the Web Service Class.

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

This line indicates that your webservice conforms to the Web Services Interopability Organization's (WS-I) Baisc Profile 1.1. The Basic Profile defines a set of rules to which your webservice must conform.

One of its rules is that the webservice may not include Method Overloading. If you run a webservice that uses method overloading, you will receive an error message telling that your service is not conform to WS-I Basic Profile v1.1. This produces error once you have equiped the ‘Web Method’ with ‘MessageName’ property.

Solution: To resolve this error, 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";
    }   
}

This will work fine.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images