How to do method overloading in WCF ?



Hi
If you are doing method overloading in WCF then we can not do directly like C#. In WSDL based world,all the operation must have unique name. So we can do this task like this

Step1:Create one WCF application and write the code in “Iservice1” file like this

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

namespace Overloading_WCF
{

[ServiceContract]
public interface IService1
{
[OperationContract(Name=”AddNo1″)]
int AddNo(int val, int val1);

[OperationContract(Name=”AddNo2″)]
int AddNo(int val, int val1, int val2);
}
}

Step 2: Implement the interface in “Service.svc”like this

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

namespace Overloading_WCF
{

public class Service1 : IService1
{

public int AddNo(int val, int val1)
{
return (val + val1);
}

public int AddNo(int val, int val1, int val2)
{
return (val + val1 + val2);
}

}
}

Step3: Compile the service and consume in application

Step4: Write the code in codebehind file like this

protected void btnSum_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client objProxy=new ServiceReference1.Service1Client();
if (txtNo1.Text != string.Empty && txtNo2.Text != string.Empty)
{
lblSum.Text = objProxy.AddNo1(int.Parse(txtNo1.Text), int.Parse(txtNo2.Text)).ToString();
}
if (txtNo1.Text != string.Empty && txtNo2.Text != string.Empty && txtNo3.Text != string.Empty)
{
lblSum.Text = objProxy.AddNo2(int.Parse(txtNo1.Text), int.Parse(txtNo2.Text),int.Parse(txtNo3.Text)).ToString();
}

}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.