Method Overloading in WCF


Method or Operation overloading is one of the most important feature of OOPS. In programming languages like C, C++ and C#, this feature is heavily used by developers. While writing Service definition you may come across when you need to overload operations.

Let us go ahead and write Service with overloaded function as following. In below ServiceContract I am creating two methods and overloading them with different parameters. To achieve overloading at Service side I set Name parameter of both function as follows:


  [OperationContract(Name = "GetIntegerData")]
  string GetData(int value);

  [OperationContract(Name = "GetStringData")]
  string GetData(string value);

So to consume service in my client application, you need to use the method name you specified in the Name attribute. The code in my client application will be:


  ServiceReference1.Service1Client client = new Client.ServiceReference1.Service1Client();
  Console.WriteLine(client.GetIntegerData(5));
  Console.WriteLine(client.GetStringData("pranay"));

Summary : You can easily use method overloading in WCF service with the help of Name field in OperationContract attribute.

Hope this will help !!!

Jay Ganesh

Advertisements

2 thoughts on “Method Overloading in WCF

    1. please share your code. There might be something missing in it. I have tested above code and it worked for me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s