MessageName property of WebMethod in Web Services


Description: As it's name tells, it describe the web service method. It describes the functionality of web method at service help page.

MessageName: It is used to give alias name to web method. This alias name uniquely identify the web method. This property is used to implement method overloading in web services. Means when web service has more than one methods with
same name, then we add MessageName property to WebMethod. When MessageName is specified, web method is identified by MessageName otherwise it is identified by method name.

At first, we create a web services without using any web method property. There are three methods in the service. 

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

namespace
WebService1
{
    /// <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 string show()
        {
            return "Hi";
        }
        [WebMethod]
        public int add(int a,int b)
        {
            return a+b;
        }
        [WebMethod]
        public int sub(int a,int b)
        {
            return a - b;
        }
    }
}
 
Run this service.

description property of webmethod

Using Description Property:
 
Now we add Description property to WebMethod. Look at the below code.

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

namespace
WebService1
{
    /// <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(Description="Show Hi") ]
        public string show()
        {
            return "Hi";
        }
        [WebMethod(Description = "Addition of Two No.")]
        public int add(int a,int b)
        {
            return a+b;
        }
        [WebMethod(Description="Subtraction of Two No.")]
        public int sub(int a,int b)
        {
            return a - b;
        }
    }
}

Then, run your service application. You will note here, that description appeared with webmethod.

description property of webmethod

Using MessageName Property:

Now, Add the 
MessageName property to WebMethod. 

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

namespace
WebService1
{
    /// <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(MessageName="ShowMessage") ]
        public string show()
        {
            return "Hi";
        }
        [WebMethod(MessageName = "Addition")]
        public int add(int a,int b)
        {
            return a+b;
        }
        [WebMethod(MessageName = "Subtraction")]
        public int sub(int a,int b)
        {
            return a - b;
        }
    }
}

Now run the service.

Output:

messagename property of webmethod


Similar Articles