Friday, February 15, 2013

Method Overloading in WebServices

As we know that Web Service is also a class just like other classes. In web service we can overload the web methods but it is not as straightforward as in class. Method overloading in the web service is little bit different. Here in this example, I will show you how to overload the web methods in We Service.

 

Overloading in Web Service

While trying to overload Web Methods of Web Services and then try to built, it will work successfully. But when we try to run or consume the service, it will show a error message. We can show this using the example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace OverloadinginWebService
{
    /// <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 Add(int a, int b)
        {
            return (a + b);
        }
        [WebMethod]
        public float Add(float a, float b)
        {
            return (a + b);
        }
    }
}

When we try to run the above code it will show the following error message-

error page

Solution of this error

Solution of this error is very easy. We have to add the MessageName  property to make difference between methods and change [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] to [WebServiceBinding(ConformsTo = WsiProfiles.None)]. So modify the code with adding the MessageName property as written below.
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace OverloadinginWebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.None)]
    [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(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);
        }
    }
}

Now run the service.

Output:-
successfully run web service
That's it. Happy coding!! :)
kick it on DotNetKicks.com

No comments:

Post a Comment

^ Scroll to Top