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
float, etc.) and the
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.
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.
Add method within the web service based on the number of parameters. Add one more parameter to the second
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
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
Now view the web service in a browser window. We will get a different error.
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
BaseProfile1_1. Let’s change that to
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
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
Arun Ramachandran (http://BestTEchnologyBlog.Com)