Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

ASP.NET Web Services – How to Overload a Web Method?

5.00/5 (3 votes)
24 Aug 2014CPOL2 min read 27.1K  
WebMethod overloading in ASP.NET web services

In the last blog post, we have discussed about different attribute properties of a WebMethod. In this article, we will go over WebMethod overloading in ASP.NET Web Services.

This is a continuation of the previous article. So please go through that before proceeding with this article to get a clear idea. You can read that article here.

Method overloading allows a class to have multiple methods with the same names, but with different signatures. 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 MessageName property. This property is used to uniquely identify the individual XML web service methods.

Let’s understand this with an example. First of all, modify the CalculatorWebService class like below. Create 2 identical Add methods. They have the same name and the same number of parameters.

WebServices1 - Copy

Let’s build the solution. Obviously, we will get a compile time error. It is because if we want to overload methods, they have to differ at least in number, type or kind of parameters.

WebServices2

Let’s overload Add method within the web service based on the number of parameters. Add one more parameter to the second Add method.

WebServices3

Build the solution and view the web service in a browser window. But we will get an error. The error is stating that the Add method which uses 3 parameters and the Add method which uses 2 parameters are using same MessageName Add.

WebServices4

But if we look at the methods, we didn’t specify any MessageNames. So if we didn’t specify any MessageNames, the name of the method will be taken as the MessageName by default. Here, CalculatorWebService doesn’t know how to uniquely identify these 2 methods. So, there needs to be 2 different message names. For this, we need to use the MessageName property of the WebMethod attribute. Let’s specify the MessageName for first Add method as Add2Numbers.

WebServices5

Now view the web service in a browser window. We will get a different error.

WebServices6

This is because when we add a web service to the project, Visual Studio IDE will auto generate a WebServiceBinding attribute. By default, it is setting WsiProfiles to BaseProfile1_1. Let’s change that to None.

WebServices7

Build the solution and view the web service in a browser. Now we can see both the Add methods. Both of them have same names. But MessageName of the second Add method is Add2Numbers.

WebServices8

If we look at the SOAP request messages, we can see that name of the first method is Add and name of the second method is Add2Numbers.

WebServices9

WebServices10

Reference

Arun Ramachandran (http://BestTEchnologyBlog.Com)

Image 11 Image 12

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)