Method Overloading and Overriding in WCF?

First question comes in every one mind that can we overload or override method in WCF? We will know about this in this article.

 

Method Overloading:

Please click below link to read concept of method overloading in C#.

Method overloading

Method overloading is possible in C#. You can think that it is possible in WCF also.

But you will be surprised that WCF does not support method overloading. We will create WCF service and try to overload method.

 

Step1: Open visual studio. Go to File->New->New project

Step2: Click Installed->Templates->Visual C#->WCF

Step3: Enter Name of WCF project as MethodOverloadingAndOverriding or any name you can enter. Please see below image to create WCF project.

 

image

 

Step4: IService1 interface is created. Delete class CompositeType, GetData() and GetDataUsingDataContract() method and add below mention two GetData() methods.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

 

namespace MethodOverloadingAndOverridingWCF
{
    //Service interface IService1
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

         [OperationContract]
        string GetData(string value);

     
    }

}

 

Step5: Click on Service1.svc file from project. Delete GetData() and GetDataUsingDataContract() from Service1 Class.

Step6: Add two GetData() methods in class Service1 as mention below.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MethodOverloadingAndOverridingWCF
{
  
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format(“GetData {0}”, value);
        }
              
        public string GetData(string value)
        {
            return string.Format(“GetData {0}”, value);
        }
    }
}

 

Step7: Compile the project and run the WCF application by pressing f5. You will get System.InvalidOperationException as shown below.

 

image

 

As you saw, method overloading is not happening in WCF. But there is way to overload method. WCF provide Name property of OperationContractAttribute. You can overload method using this Name property.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

 

namespace MethodOverloadingAndOverridingWCF
{
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format(“GetData {0}”, value);
        }
              
        public string GetData(string value)
        {
            return string.Format(“GetData {0}”, value);
        }
    }
}

Now add name property of OperationContractAttribute as shown below in IService1 interface. Build the WCF project and run it.

 

  [ServiceContract]
   public interface IService1
   {

       [OperationContract(Name=”GetIntData”)]
       string GetData(int value);

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

   }

Client App: Create a console application. Right click on service reference of console app project.

Enter the WCF service URL as shown in below dialog and then press ok.

 

 

image

 

Client Code:

Add below code to Client console app after adding service reference.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClientApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            string result1 = client.GetIntData(10);
            string result2 = client.GetStringData(“ram”);
            Console.WriteLine(result1);
            Console.WriteLine(result2);

            Console.ReadLine();

        }
    }
}

 

Output will be 10 and ram.

 

Method Overriding:

Please click below link to read about method overriding.

Method Overriding

Method overriding is not possible in WCF.

 

Conclusion:

  • Method can be overloaded in WCF using name property of OperationContractAttribute.
  • Method Overriding is not possible in WCF.

 

Please provide your comments and suggestions.

Thanks for reading. Happy coding Smile

Be the first to comment

Leave a Reply

Your email address will not be published.


*