WCF Method Overloading

How do you do Method Overloading in WCF? In this article,I am using Visual Studio 2008 building a WCF service and for simplicity, NET TCP Binding is used.

How do you do Method Overloading in WCF? This is the most asked question in the interviews. So, I thought of writing a small article on the method overloading so it may help others. I am using Visual Studio 2008 building a WCF service and for simplicity NET TCP Binding is used.
 
Before digging into the code, let us understand what overloading means and whether we can overload a method in WCF.
 

Method Overloading

 
Method Overloading is a feature that allows creation of multiple methods with the same name but each method should differ from another in the context of input and output of the function. No two methods must have the same type parameters.
 
Either they must differ in the number of parameters or in the type of the parameters.
 
You can also refer to Method Overloading as a compile-time polymorphism since the calling method knows the address of the called method and the method addresses are resolved at compile time. This is also called as Early Binding.
 
Let us look at a small example to check how the compiler verifies if the methods are overloaded or not if two methods have the same signatures.
  1. namespace MethodOverloading  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Console.WriteLine("The sum of 4,5 is {0}", Add(4,5));  
  8.         }  
  9.         static int Add(int a, int b)  
  10.         {  
  11.             return a + b;  
  12.         }  
  13.         static int Add(int a, int b)  
  14.         {  
  15.             return a + b;  
  16.         }  
  17.     }  
  18. }  
You will see that the compiler shows an error when you call the Add(int, int) method, since it finds two methods with the same name and signatures.
 
Showing-Ambiguous-error-by-Compiler.jpg 
Figure: Showing Ambiguous error by Compiler
 
Even if we change the return type of the second method Add(int, int), still you will see the error:
  1. static double Add(int a, int b)  
  2. {  
  3.     return a + b;  
  4. }  
So, the possible type of overloads are as in the following:
  1. static int Add(int a, int b)  
  2. {  
  3.     return a + b;  
  4. }  
  5. static int Add(int a, int b, int c)  
  6. {  
  7.     return a + b;  
  8. }  
  9. static double Add(double a, double b)  
  10. {  
  11.     return a + b;  
  12. }  
  13. static double Add(double a, int b)  
  14. {  
  15.     return a + b;  
  16. }  
So, now if you run the following code you will get an output based on the data types that you have passed:
  1. Console.WriteLine("The sum of 4,5 is {0}", Add(4,5));  
  2. Console.WriteLine("The sum of 4.5,5 is {0}", Add(4.5, 5));  
  3. Console.WriteLine("The sum of 4.5,5.5 is {0}", Add(4.5, 5.5));  
  4. Console.ReadLine();
Output-of-program.jpg  
Figure: Output of program
 

Method Over Loading in WCF

 
Since Overloading is possible with C# code and so we may assume that it might be possible too in WCF as we are using C# code for service methods. But, the answer is no if you implement the overloading as shown in the previous example. WSDL does not support the same overloading concepts that are supported by C#. When you are consuming a service over HTTP/SOAP, having the same method name in your contract would mean that there is no way to determine the particular method the client can invoke.
 
For testing the code, take the example mentioned in the resource link at the beginning of the article and modify the interface IHelloWorld as in the following:
  1. [ServiceContract]  
  2. public interface IHelloWorld  
  3. {  
  4.     [OperationContract]  
  5.     string ShowData(int value);  
  6.     [OperationContract]  
  7.     string ShowData(string value);  
  8. }  
Also modify the class HelloWorld as in the following to implement the two methods:
  1. public class HelloWorld: IHelloWorld  
  2. {  
  3.  #region IHelloWorld Members  
  4.  string IHelloWorld.ShowData(int value)  
  5.  {  
  6.   return string.Format("Message received from client number {0} at {1}", value, DateTime.Now.ToString());  
  7.  }  
  8.  string IHelloWorld.ShowData(string value)  
  9.  {  
  10.   return string.Format("Message received from client number {0} at {1}", value, DateTime.Now.ToString());  
  11.  }  
  12.  #endregion  
  13. }  
Now build the solution and run the host project and you will see the following error:
 
Starting-a-new-instance-of-the-project.jpg 
Figure: Starting a new instance of the project
 
Exception-generated-by-the-compiler.jpg 
Figure: Exception generated by the compiler
 
So, the solution to implement the overloading is to specify a name property of the OperationContractAttribute as specified in the error above.
 
So, our ServiceContract code should look as in the following:
  1. [ServiceContract]  
  2. public interface IHelloWorld  
  3. {  
  4.     [OperationContract(Name = "ShowIntData")]  
  5.     string ShowData(int value);  
  6.     [OperationContract(Name = "ShowStringData")]  
  7.     string ShowData(string value);  
  8. }  
Note: If you are following the exact code in the resource link provided, then you might have to modify the program.cs file in the Client project for the service contract body.
  1.     [ServiceContract]  
  2.     public interface IHelloWorld  
  3.     {  
  4.         [OperationContract(Name = "ShowIntData")]  
  5.         string ShowData(int value);  
  6.         [OperationContract(Name = "ShowStringData")]  
  7.         string ShowData(string value);  
  8.     }  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             IHelloWorld proxy = ChannelFactory<IHelloWorld>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9000/HelloWorld"));  
  14.             Console.WriteLine(proxy.ShowData("Hello World From Client"));  
  15.             Console.ReadLine();  
  16.         }  
  17.     }  
Name and click "Add Service Reference" item. Now click on the "Discover" button and you will see the address generated by Visual Studio for you. Click OK and the service will be added to your client project.
 
Adding-service-reference-using-MEX.jpg 
Figure: Adding a service reference using MEX
 
Showing-the-tree-structure-of-service-reference.jpg 
Figure: Showing the tree structure of service reference
 
You should see the following in your WSDL:
 
Operation-names-in-the-WSDL.jpg 
Figure: Operation names in the WSDL
 
Now go to the Program.cs file in the Client project and change the code as in the following:
 
Intellisense-showing-the-available-methods-of-the-client.jpg 
Figure: Intellisense showing the available methods of the client
  1. namespace Client  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ServiceReference1.HelloWorldClient client = new Client.ServiceReference1.HelloWorldClient();  
  8.             Console.WriteLine(client.ShowIntData(2));  
  9.             Console.WriteLine(client.ShowStringData("string data"));  
  10.             Console.ReadLine();  
  11.         }  
  12.     }  
  13. }  
Now if you run the program you should see the output without error. So, Overloading is not the same as when we write code for normal classes and it can be done for WCF services using a Name attribute in the OperationContract.
 
Hope you liked this article and happy coding!!!