Overloading WebMethod In ASP.NET Web Service - Part Five

Method Overloading
 
Method overloading allows us to have two or more methods of the same name but with different arguments (signature) in the same class. The concept of method overloading in Web Method of Web Service is different, as we use MessageName property to achieve method overloading in a Web Service. MessageName property is basically used to uniquely identify the individual XML Web Service methods.

Note
 
This is a very common interview question as well:  Is it possible to overload a web method in a web service? The answer is yes, you need to use MessageName property for this.

I’ve discussed MessageName property in my previous article. Before starting this article, follow the journey of the Web Services series,

I’m going to start my work with my previous Visual Studio solution that I’ve been working with. Hence, let’s flip to Visual Studio.

In order to achieve method overloading, let’s make a copy of AddName method. These two AddName methods are identical as they have the same name, they have the same number type,  and the same kind of parameters.



Now let’s build your Web Service, see what happened.
 


We got a compilation error, which is because if we want to overload methods, they are different- at least numbers, types or kind of the parameters. Thus, you need to overload these AddName methods within the Web Service, based on the number of parameters.
 


Therefore, the first one, AddName method, has two parameters. Let’s makea second AddName method, which has three parameters. Thus, in the second method, make three parameters, so the first method is going to add two strings and the second method is going to add three strings.
 


Now, again build this Web Service.
 


Build has been successful.
 


Now, let’s view this web Service in a Browser Window. Right click Web Service and choose View in the Browser.
 


Consider the following screenshot, where we got an error with AddName method, which has three string parameters and the other one has two string parameters, using the same message name, which is AddName.
 


The problem is that Web Services are using the same message name for both the methods so our Web Service doesn’t know how to uniquely identify these two methods, so we need to specify the MessageName property of the Web method attribute to these methods. Let’s add this property on both of them.

Now, again let’s view this Web Service in a Browser Window. Right click Web Service and choose View in the Browser.


Oops, consider the image, below, as we got ta totally unrelated error.
 


This error is coming, because when we added this MyWebService.asmx to this project WebServiceDemo, Visual Studio integrated development environment (IDE) and has automatically generated a Web Service binding attribute for the user. Look at this screenshot -
 


It is by default set to WsiProfiles.BasicProfile1_1. You need set to None it like:
 


Again, run your page. Probably, it should be all right this time.
 


Now, you can see both the methods, which have the same name here, but for one method, we have a message name. Click AddName method which holds Adding3Strings MessageName property.
 


Within the SOAP request message, the message is basically “Adding3Strings”, this means our Web Service has a way to uniquely identify these methods within the Web Service.
 


Now, click the AddName method that holds the Adding2Strings MessageName property.
 


As you can see in the screenshot, within the SOAP request message, the message is basically “Adding2Strings”.

Notice that using MessageName property, we can see the SOAP request message as MessageName property, but follow the first article of this series, where you can see we have our method name in the SOAP request message. This is the reason why our Web Service has a way to uniquely identify these methods within the Web Service.
 


You can also check your result on the Browser Window. Put two strings and click Invoke button.
 


We can see it on the Browser, as following in the screenshot:
 


You can check the  second method in the Browser as well.
 


Finally, we got our output as a string.
 


Thanks for reading this article. Stay tuned with us for more on Web Services.

Up Next
    Ebook Download
    View all
    Learn
    View all