Pages

Thursday, November 18, 2010

Web Services - Method Overloading

This is not as same as we have in the Class. It will be build successfully but when we use or consume, it will generate error message - "Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods". 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.
To overcome this just do the following: Also add the following line above the class

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[WebMethod(MessageName = "Print1", Description = "Print 1", EnableSession = true)]
    public int A(int a, int b)
    {         return 1;     }
    [WebMethod(MessageName = "Print2", Description = "Print 2", EnableSession = true)]
    public int A(int x)
    {         return 2;     }
Reason
By passing the 'MessageName Property’ it changes the method name in the WSDL as shown below:
<wsdl:operation name="A">
            <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Print 1</wsdl:documentation>
            <wsdl:input name="Print1" message="tns:Print1SoapIn" />
            <wsdl:output name="Print1" message="tns:Print1SoapOut" />
</wsdl:operation>
<wsdl:operation name="A">
            <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Print 2</wsdl:documentation>
            <wsdl:input name="Print2" message="tns:Print2SoapIn" />
            <wsdl:output name="Print2" message="tns:Print2SoapOut" />
</wsdl:operation>

No comments:

Post a Comment