Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • WebMethod overloading in asp net web services

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 639
    Comment on it

    Hello all,

    Working with .net web services, at time we have to overload our WebMethod, and the process of WebMethod overloading is very much same to the method overloading in C#.

    Method overloading allows a class to have multiple methods with the same name, but with the different signature.
    So in C#, methods can be overloaded based on the number, type(int, float etc) and the kind (Value, Ref or Out) of parameters.

    WebMethods in a web Service can also be overloaded, using Message Name property. This property is used to uniquely identify the individual XML Web service method.

    Web Service identify the methods by message name, that is the name of the methods by default (if we haven't defined the Message name attribute of the Web Method).
    So even if you had defined the the methods with different parameters (overloaded methods), it will still give error.

    So to get rid of this error, we have to define Message Name property of the Web Methods, which will distinguish both the method.

     [WebMethod (MessageName="AddTwoNumber")]
    

    We also have to change WebServiceBinding attribute of web service and set

      [WebServiceBinding(ConformsTo = WsiProfiles.None)]
    

    Following is the code packet for the WebMethod overloading in Web Service :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    namespace WebServiceApp
    {
        /// <summary>
        /// Summary description for GetData
        /// </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 GetData : System.Web.Services.WebService
        {
    
            [WebMethod (MessageName="AddTwoNumber")]
            public int Add(int firstNumber, int secondNumber)
            {
                return firstNumber + secondNumber;
            }
    
            [WebMethod]
            public int Add(int firstNumber, int secondNumber, int thirdNumber)
            {
                return firstNumber + secondNumber + thirdNumber;
            }
        }
    }   
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: