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.
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.
Related Blogs -
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.
Related Blogs -
- Web Services, SOAP and RESTful Services
- Get JSON data in Web Service
- How to create a secure Web Service?
- How to overload a web method in Web Service?
- Web Service in InfoPathForm - SharePoint 2010
Comments
Post a Comment
Dear Readers, Please post your valuable feedback in the comment section if you like this blog or if you have any suggestions. I would love to hear the same from you. Thanks