Is this possible?
It is possible to overload a method in WCF. However, when you want to expose overloaded methods as operations in a WCF contract, there is more to it than simply writing two or more methods with the same name.
Consider the following example:
[ServiceContract]
public interface ICalendarService
{
[OperationContract]
ScheduledEvent[] GetScheduledEvents(DateTime date);
[OperationContract]
ScheduledEvent[] GetScheduledEvents(DateTime start, DateTime end);
}
Now, try to actually implement this interface and host it as a WCF service. An InvalidOperationException will be raised when you invoke ServiceHost.Open.
As far as the C# compiler is concerned, this is a completely legal piece of code. The interface contains two methods. The methods have the same name, but their signatures are unique since their parameters differ. The interface and methods are decorated with attributes. However, WCF doesn’t have any special hooks into the compiler. Consequently, the compiler doesn’t have any special knowledge about the attributes. It is simply going to emit their information in the assembly metadata.
When WCF attempts to start the service, it essentially interrogates the metadata to generate a WSDL contract. WSDL is all about technology neutral, message based communication. It doesn’t support object-oriented concepts such as inheritance and overloading. (Actually, I think the earliest versions of WSDL allowed overloading to a certain extent, but it has since been removed in the newer versions.) So, WCF basically detects there are two methods with the same name and raises an exception to indicate this isn’t allowed even though the code compiled without any errors.
Regardless, WCF still provides the ability to overload methods. This can be achieved by using the name property of the OperationContract attribute. By default, the name of the method will be used for the value of the name property. However, you can explicitly set the value of the name property to provide a unique value. Under the covers, WCF will use the value you supply as the name of the corresponding operation in the generated WSDL.
Here is our previous example revised to use alias method names:
[ServiceContract]
public interface ICalendarService
{
[OperationContract(Name = "GetScheduledEventsByDate")]
ScheduledEvent[] GetScheduledEvents(DateTime date);
[OperationContract(Name = "GetScheduledEventsByDateRange")]
ScheduledEvent[] GetScheduledEvents(DateTime start, DateTime end);
}
You may be wondering how the method will appear to the consumer of the service. Will it appear as two different names (based on the the alias) or as overloaded methods with the same name? This will depend upon the proxy class used by your client. If you use the proxy class that is automatically generated by svcutility.exe, the alias method names will be used. However, you can manually edit the generated proxy class to achieve the appearance of overloaded methods on the client as well. This can be accomplished by applying the same attributes to the methods defined in the interface that is used by the proxy class.
Original article from Jeff Barnes