Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

ASP.Net Web Services – How to overload a web method?

, 24 Aug 2014
Rate this:
Please Sign up or sign in to vote.
CodeProject 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 continuation of the previous article. So please go through that before proceeding this article to get a cle

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 continuation of the previous article. So please go through that before proceeding this article to get a clear idea. You can read that article here.

Method overloading allows a class to have multiple methods with 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 same name and 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 need 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).

Comments and Discussions

 
Questionwhat if Pinmemberthewazz29-Aug-14 16:51 
GeneralMy vote of 5 PinmemberCarsten V2.024-Aug-14 22:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140905.1 | Last Updated 25 Aug 2014
Article Copyright 2014 by Arun Ramachandran India
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid