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.
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:
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 ing 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>
Resources