Understand WCF: Part 6: Function Overloading in WCF Application

In this article, we will learn the concepts of function overloading in WCF applications.

Welcome to the Understand WCF article series, in this series we are talking about WCF applications. Previous articles explained a few important concepts of WCF applications. You can read the full series here:
In this part we will learn the concepts of function overloading in WCF applications. I hope you are familiar with the concept of function overloading, but the concept of function overloading in .NET is a little different in WCF applications. In traditional .NET applications (read Windows app or Web app) we can implement the concept of function overloading by passing various types of parameters where the function name should be the same.
 
Try to understand the following example.
 
Code for service contract.
 
This is the code for the service contract (interface implementation). Try to understand that we have been given the same attribute ("[OperationContract]") in both function declerations.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. namespace WCF  
  9. {  
  10.     [ServiceContract]  
  11.     public interface IService1  
  12.     {  
  13.         [OperationContract]  
  14.         string AddName(string Name, string LastName);  
  15.         [OperationContract]  
  16.         string AddName(string Name, string MiddleName, string LastName);  
  17.     }  
  18. }  
Implementation of class
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;   
  8.   
  9. namespace WCF  
  10. {  
  11.     public class Service1 : IService1  
  12.     {  
  13.         public string AddName(string Name, string LastName)  
  14.         {  
  15.             return Name + LastName;  
  16.         }  
  17.         public string AddName(string Name, string MiddleName, string LastName)  
  18.         {  
  19.             return Name + MiddleName + LastName;  
  20.         }  
  21.     }  
  22. }  
Here we have implemented an overloaded version of the AddName() function. The first function takes two arguments and the second function is takes three arguments. We will now try to build the application.
 
Here is sample output. We see that it throws an exception. The reason is we cannot use the same name in a function in a WCF application.
 
Exception handling in WCF 
 

How to implement Operator overloading

 
We can implement Operator overloading by specifying the proper attribute in the operation contract (within an interface). Try to understand the following example.
 
The following is a modified version of the Service contract.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. namespace WCF  
  9. {  
  10.     [ServiceContract]  
  11.     public interface IService1  
  12.     {  
  13.         [OperationContract(Name = "NameSurname")]  
  14.         string AddName(string Name, string LastName);  
  15.         [OperationContract(Name = "NameMiddleNameSurname")]  
  16.         string AddName(string Name, string MiddleName, string LastName);  
  17.     }  
  18. }  
The following is the implementation in the class:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;   
  8. namespace WCF  
  9. {  
  10.     public class Service1 : IService1  
  11.     {          
  12.         public string AddName(string Name, string LastName)  
  13.         {  
  14.             return Name + LastName;  
  15.         }  
  16.         public string AddName(string Name, string MiddleName, string LastName)  
  17.         {  
  18.             return Name + MiddleName + LastName;  
  19.         }  
  20.     }  
  21. }  
Now to implement client code to consume the service.
 
We will now implement one simple client code to consume the service. Here we have created one sample console application and consumes the service.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Diagnostics;  
  6. using System.Collections;  
  7. using Client;  
  8. using System.ServiceModel;   
  9. namespace Client  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             Client.ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();  
  16.             string name = obj.NameSurname("Sourav"" Kayal");  
  17.             Console.WriteLine("NameSurname Function:- " + name);  
  18.             name = obj.NameMiddleNameSurname("Sourav ""Kumar " , " Kayal");  
  19.             Console.WriteLine("NameMiddleNameSurname function :- " + name);  
  20.             Console.ReadLine();  
  21.         }  
  22.     }  
  23. }  
Here is sample output.
 
WCF Exception handlin 
 
Conclusion
 
In this quick article, we learned to implement function overloading in WCF applications. Hope you have understood the concept. In a future article, we will concentrate more on WCF.