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.
[ServiceContract]
public interface IService1
{
[OperationContract( Name ="CircleArea")] // Define Name property of the OperationContract attribute
double Area(double r);
[OperationContract( Name ="RectangleArea")] // Define Name property of the OperationContract attribute
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 = 3.14 * r * r ;
return c;
}
public int Area(int length, int width)
{
int c = length * width;
return c;
}
}
}
Testing the Service
Press F5 to run the service. A WCF Test Client form will be displayed and it will load the service.
Now double-click the CircleArea() method under IService1. The CircleArea tab will be displayed.
The service was added successfully.
Now open the service in the browser.
Now right-click on the service1.vcs -> open in browser:
Now click on the view in the browser.
URL
http://localhost:28282/Service1.svc
Step 3: Create console Application (Accessing the Service)
Now, you have to create a console Application.
- Go to Visual Studio 2010
- New-> Select a console application
- Click OK
Now right-click on the application to add the service reference.
When we click on the add the service reference the following window will be opened:
Now paste the above URL in the address and click on the go Button.
Click on the Ok Button. Now the reference has been added in the Solution Explorer.
Now add the following code in the .cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ServiceReference1.Service1Client objservice = new ServiceReference1.Service1Client();
Console.Write("Area of the Circle :");
Console .WriteLine(objservice.CircleArea(5));
Console.Write("Area of the Rectangle :");
Console.WriteLine(objservice.RectangleArea(3, 4));
}
}
}
Now run the application.
Press CTRL+F5 to run the application: