WCF method Overloading

WCF Method Overloading

Hello folks, I have already explained regarding Transfer large file using WCF and The remote server returned an unexpected response: (413) Request Entity Too Large. Today I am going to explain another good concept of WCF Method Overloading.

As you all know method overloading is supported by .net Framework. So WCF also supports method overloading, but how could we achieve WCF method Overloading!!! Let’s have an example and have a look  deeply in this concept.

We have two methods named with “WcfMethodOverloading” as shown below:

IService1.cs

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

namespace WCFOverloading
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string WcfMethodOverloading(string strData);

        [OperationContract]
        string WcfMethodOverloading(int iData);
    }
}

Service1.svc.cs

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

namespace WCFOverloading
{
    public class Service1 : IService1
    {
        public string WcfMethodOverloading(string strData)
        {
            return "Wcf Overloading Method 1";
        }
        public string WcfMethodOverloading(int iData)
        {
            return "Wcf Overloading Method 2";
        }
    }
}

Now if we will compile this project, it will not throw any error. But when we will run it,we would have below error screen:

Wcf-Method-Overloading
Wcf-Method-Overloading

To remove this error and run our WCF service we need to do small change in our IService1.cs 

1.[OperationContract(Name = “WcfMethodOverloading1”)]

2. [OperationContract(Name = “WcfMethodOverloading2”)]

Just we have to give name of OperationContract as shown below full page code:

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

namespace WCFOverloading
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract(Name="WcfMethodOverloading1")]
        string WcfMethodOverloading(string strData);

        [OperationContract(Name = "WcfMethodOverloading2")]
        string WcfMethodOverloading(int iData);
    }
}

Now you could call method from client as shown below:

Wcf-Method-Overloading-Client
Wcf-Method-Overloading-Client

Don’t forget to share your views.

 

2 comments

Leave a Reply