Issue During Method Overloading in WCF
Problem
I have encountered a common problem during method overloading in WCF. In a WCF service, method overloading does not occur directly through WCF Service. Here I am creating a WCF service to describe method overloading in a WCF service. I am creating two functions with the same name: Area. The first "Area" function has one parameter and the second method has two parameters. Now we create a WCF service.
Iservice1.cs File
Now we create functions in the OperationContract section of the Iservice1.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace SessioninWCF
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
double Area(double r);
[OperationContract]
int Area(int length, int width);
}
}
Service.svc.cs File
In this file we define the definition of the functions double Area(double r) and int Area(int length, int width). And replace the code with the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace SessioninWCF
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
public double Area(double r)
{
double c = 1 / 3 * 3.14 * r * r * r;
return c;
}
public int Area(int length, int width)
{
int c = length * width;
return c;
}
}
}
It will generate an error because the WCF service does not support method overloading directly. Now run the service. The following error will occur:

Solution (to Achieve Method Overloading in WCF)
WCF still provides the ability to overload methods. This can be done using the Name property of the OperationContract attribute to alias the operation. 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.
Now go into the Iservice1.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace SessioninWCF
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.











Rohatash KumarPosted Oct 5, 2012, 6:07 AM
Thanks.Vijay
Vijay DahitePosted Aug 22, 2012, 12:56 AM
Nice and clean post....Pleaese share some more...Thanks for sharing