Pages

Saturday 20 April 2013

Function Overloading in Web Services

Overloading Web Services

While trying to overload Web Methods in Web Services and after doing the build, it will work successfully. But when we try to run or consume, it will show an error message. We can show this using an example.
namespace TestOverloadingWebService
{ 
    [WebService(Namespace = "http://tempuri.org/", Description=" <b> Function 
    overloading in Web Services </b>")] 
public class OverloadingInWebService : System.Web.Services.WebService 
{ 
    [WebMethod()] 
    public int Add(int a, int b) 
    { 
        return (a + b); 
    } 
    [WebMethod()] 
    public float Add(float a, float b) 
    { 
        return (a + b); 
    } 
} 
}
In the above example, we made one web service having class OverloadingInWebService. In the Web Service, we have added attribute Description, which is used to describe the web service purpose to client. In the above Web Service, we have two overloaded WebMethods:
public int Add(int a, int b) 
and
public float Add(float a, float b)
While running this Web service, it will show the following runtime error.
ViewError.JPG

Solution for the Above Error

The procedure to solve this problem is very easy. Start each method with a Web Method attribute. Add Description property to add a description of web method and MessageName property to change web method name.
[WebMethod(MessageName = "<name>", Description = "<description>")]
namespace TestOverloadingWebService 
{ 
    [WebService(Namespace = "http://tempuri.org/", Description=" <b> Function 
    overloading in Web Services </b>")] 
public class OverloadingInWebService : System.Web.Services.WebService 
{ 
    [WebMethod(MessageName = "AddInt", Description = "Add two integer 
        Value", EnableSession = true)] 
    public int Add(int a, int b) 
    { 
        return (a + b); 
    } 
    [WebMethod(MessageName = "AddFloat", Description = "Add two Float  
        Value", EnableSession = true)] 
    public float Add(float a, float b) 
    { 
        return (a + b); 
    } 
} 
}

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>
 
Other Example 
 
Creating XML Web Service in .Net
Use the following steps to create a web service.
  • Go to Visual Studio 2010 and create a New Project.
img1.gif
  • Select .NET Framework 3.5.
  • Create an ASP.NET Web Service Application.
  • Give it a name and click ok button.
img2.gif
Add the following method in the Service.cs file.
[WebMethod] public int AddNumber(int a, int b) {     return (a + b); } [WebMethod] public int AddNumber(int a, int b, int c) {     return (a + b + c); }
The complete Service.cs file is:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;namespace WebMethodOverlodding {     /// <summary>    /// Summary description for Service1    /// </summary>    [WebService(Namespace = "http://tempuri.org/")]     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]     [System.ComponentModel.ToolboxItem(false)]     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.     // [System.Web.Script.Services.ScriptService]    public class Service1 : System.Web.Services.WebService    {         [WebMethod]         public int AddNumber(int a, int b)         {            return (a + b);         }         [WebMethod]         public int AddNumber(int a, int b, int c)         {            return (a + b + c);         }     } }
In the above Web Service, we have two overloaded WebMethods. This Web Service would compile fine. Run the WebService in the browser. That should give an error saying that the AddNumbers() methods use the same message name 'AddNumbers' and asking to use the MessageName property of the WebMethod. like as.
img3.gif
Solution for the Above Error
The procedure to solve this problem is very easy. Start each method with a Web Method attribute. Add the MessageName property to the WebMethod attribute as shown below.
[WebMethod] public int AddNumber(int a, int b) {      return (a + b); } [WebMethod (MessageName="AddThreeNumbers")] public int AddNumber(int a, int b, int c) {      return (a + b + c); }
The complete Service.cs file is look like as.
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;namespace WebMethodOverlodding {     /// <summary>    /// Summary description for Service1    /// </summary>    [WebService(Namespace = "http://tempuri.org/")]     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]     [System.ComponentModel.ToolboxItem(false)]     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.     // [System.Web.Script.Services.ScriptService]    public class Service1 : System.Web.Services.WebService    {       [WebMethod]       public int AddNumber(int a, int b)         {            return (a + b);         }      [WebMethod (MessageName="AddThreeNumbers")]      public int AddNumber(int a, int b, int c)         {            return (a + b + c);         }     } }
Now again run the above web service by pressing the F5 key; we will then see:
IMG4.gif
img5.gif
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 AddNumber but the input method name and output method name is different. The same will apply for second method also.
<wsdl:operation name="AddNumber">     <wsdl:input message="tns:AddNumberSoapIn"/>     <wsdl:output message="tns:AddNumberSoapOut"/>  </wsdl:operation>  <wsdl:operation name="AddNumber">      <wsdl:input name="AddThreeNumbers" message="tns:AddThreeNumbersSoapIn"/>      <wsdl:output name="AddThreeNumbers" message="tns:AddThreeNumbersSoapOut"/>  </wsdl:operation>
 

No comments:

Post a Comment