How to overload a web method in web service - ASP.Net

We can define Method Overloading as , Same Methods name with different type of parameter or different set of parameters.

Web method overloading is not straight forward in use.

Suppose there are 2 methods in a web service.

 public string GetEmployee(int employeeId)
    {
        string empName = string.Empty;
        //write code and get employee name

        return empName;
    }

    [WebMethod]
    public string GetEmployee(string emailId)
    {
        string empName = string.Empty;
        //write code and get employee name

        return empName;
    }

This code will compile successfully but will give error at runtime. Please look at below screen shot.


You just need to add message to both of the method to identify.

So new code will look like this:

 [WebMethod(MessageName="Get By Employee ID")]
    public string GetEmployee(int employeeId)
    {
        string empName = string.Empty;
        //write code and get employee name

        return empName;
    }

    [WebMethod(MessageName = "Get By Employee Email ID")]
    public string GetEmployee(string emailId)
    {
        string empName = string.Empty;
        //write code and get employee name

        return empName;
    }
    

Also if you are new to web service then don't forget to change this  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
to
[WebServiceBinding(ConformsTo = WsiProfiles.None)] from top of the page in service code behind.

Comments

Popular posts from this blog

CAML Query in SharePoint 2010

Calling REST APi from server side code - C#

All about SharePoint 2010 Content Type Hub

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX