Click here to Skip to main content
12,401,060 members (62,486 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.2K views
5 bookmarked
Posted

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

, 24 Aug 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

License

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

Share

About the Author

Arun Ramachandran India
Software Developer
India India
Arun Ramachandran is a Software Engineer having hands on experience in different Microsoft Technologies who is presently working in Experion Technologies, India. He has written over 95 articles on the subject on his blog at http://BestTEchnologyBlog.com. Along with 3 years of hands on experience he holds a Master of Computer Applications degree from Cochin University of Science & Technology (CUSAT).

You may also be interested in...

Comments and Discussions

 
Questionwhat if Pin
thewazz29-Aug-14 16:51
memberthewazz29-Aug-14 16:51 
GeneralMy vote of 5 Pin
Carsten V2.024-Aug-14 22:05
memberCarsten V2.024-Aug-14 22:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 25 Aug 2014
Article Copyright 2014 by Arun Ramachandran India
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid