Issue While Achieve Method Overloading in WCF

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:

 

img1.jpg

 

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.

img2.jpg

Now double-click the CircleArea() method under IService1. The CircleArea tab will be displayed.

img3.jpg

The service was added successfully.

Now open the service in the browser.

Now right-click on the service1.vcs -> open in browser:

img4.jpg

Now click on the view in the browser.

img5.jpg

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

img7.jpg

Now right-click on the application to add the service reference.

img8.jpg

When we click on the add the service reference the following window will be opened:

img9.jpg

Now paste the above URL in the address and click on the go Button.

img10.jpg

Click on the Ok Button. Now the reference has been added in the Solution Explorer.

img11.jpg

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:

 

img12.jpg


Similar Articles