Overloaded WebMethods

Question: We are in the process of developing an application that will be used internally. We have just started coding using Visual Studio 2005. Part of the design uses overloaded functions and methods. This works fine until I start building some Web Services that we need. For some reason I keep getting the error that the message name isn’t unique. Is overloaded WebMethods not supported? Or am I doing something wrong?

Answer: Overloaded WebMethods are actually supported. However, you have to add some additional attributes to your WebMethod calls.

For example, assume you have a simple Web Service that exposes two overloaded LookupCustomer Webmethods as shown below.

When you start this up you are probably receiving the following error message

 

This error is really due to the facts that that the auto generated message names aren’t unique between the two functions.

In order to resolve this we can add the MessageName attribute to the exposed WebMethods. It’s also a good idea to include the Description attribute as well. Although, not required it does help when trying to figure out what an exposed method is doing.

If you add these attributes to your code it will then look like the following.

When this is run you now see a slightly modified test page as shown below.

 

The addition of these two attributes has modified the generated WSDL as shown below to create a unique message name.

***Note***
Overloaded methods are directly not supported by the WSI basic profile. If using Visual Studio 2005 make sure to set the conformance claims to none