A method can be overloaded on the basis of type of parameters, no of parameters, and an order of parameters.
Let, us consider the below example:
Let, us consider the below example:
File->New Project-wcf service application
Delete the two Files
Add->New Item
Write the following code in Iservice1.cs
namespace WcfService2
{
// 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]
void DoWork();
[OperationContract]
string Display();
[OperationContract]
string Display(string name);
}
}
Service1.cs
public class Service1 : IService1
{
public void DoWork()
{
}
public string Display()
{
return "Welcome";
}
public string Display(string name)
{
return string.Format("Welcome {0}", name);
}
}
Build the service, and Go to .svc file, and view in browser
and select your browser and see the output as shown below.
Now, modify the below code by using Name Attribute as shown below:
namespace WcfService2
{
// 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]
void DoWork();
[OperationContract(Name = "Dispaly")]
string Display();
[OperationContract(Name = "Display name")]
string Display(string name);
}
}
Now, run and see the output
By default WSDL does not support operational overloading, but this can be done using the Name property of OperationContract.
Example 2:
Iservice1.cs
Service1.cs
namespace WcfService2
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public void DoWork()
{
}
public int Addition(int x, int y)
{
return x + y;
}
public double Addition(double x, double y)
{
return Convert.ToDouble(x) + Convert.ToDouble(y);
}
}
}
Build the service. and see the output.
Consuming the WCF Service.
Add the service Reference first as shown below.
Add->Service Reference
and Click on Ok Button.
Design the Form as Follows:
.cs page:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
int a=Convert.ToInt32(txtFirstNumber.Text);
int b = Convert.ToInt32(txtLastNumber.Text);
dynamic result=obj.SumUsingInt(a, b);
lblmessage.Text = Convert.ToString(result);
}
}
output:
Write the following code in Iservice1.cs
namespace WcfService2
{
// 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]
void DoWork();
[OperationContract]
string Display();
[OperationContract]
string Display(string name);
}
}
Service1.cs
public class Service1 : IService1
{
public void DoWork()
{
}
public string Display()
{
return "Welcome";
}
public string Display(string name)
{
return string.Format("Welcome {0}", name);
}
}
Build the service, and Go to .svc file, and view in browser
and select your browser and see the output as shown below.
Now, modify the below code by using Name Attribute as shown below:
namespace WcfService2
{
// 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]
void DoWork();
[OperationContract(Name = "Dispaly")]
string Display();
[OperationContract(Name = "Display name")]
string Display(string name);
}
}
Now, run and see the output
By default WSDL does not support operational overloading, but this can be done using the Name property of OperationContract.
Example 2:
Iservice1.cs
Service1.cs
namespace WcfService2
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public void DoWork()
{
}
public int Addition(int x, int y)
{
return x + y;
}
public double Addition(double x, double y)
{
return Convert.ToDouble(x) + Convert.ToDouble(y);
}
}
}
Build the service. and see the output.
Consuming the WCF Service.
Add the service Reference first as shown below.
Add->Service Reference
and Click on Ok Button.
Design the Form as Follows:
.cs page:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
int a=Convert.ToInt32(txtFirstNumber.Text);
int b = Convert.ToInt32(txtLastNumber.Text);
dynamic result=obj.SumUsingInt(a, b);
lblmessage.Text = Convert.ToString(result);
}
}
output:
No comments:
Post a Comment
Thank you for visiting my blog