In this article, I’m create a Calculator using WCF.
Description
This is simple calculator that contains four basic mathematical functions for common calculation.
Step 1: Start Visual studio 2012>> click New project>> WCF service Application
Step 2: Give appropriate name to your service and click on OK button.
Step 3: Write in 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 WcfService1
{
// 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
{
//Data contract is not required since both parameter.
[FaultContract(typeof(Service1))]
[OperationContract]
[LoaderOptimization(LoaderOptimization.MultiDomain)]
double Add(double firstNum, double secondNum);
[OperationContract]
double Mul(double firstNum, double secondNum);
[OperationContract]
double Sub(double firstNum, double secondNum);
[OperationContract]
[FaultContract(typeof(Service1))]
double Div(double firstNum, double secondNum);
}
}
Step 4: Write 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 WcfService1
{
// 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 double Add(double firstNum, double secondNum)
{
return (firstNum + secondNum);
}
public double Mul(double firstNum, double secondNum)
{
return (firstNum * secondNum);
}
public double Sub(double firstNum, double secondNum)
{
return (firstNum - secondNum);
}
public double Div(double firstNum, double secondNum)
{
return (firstNum / secondNum);
}
}
}
Step 5: Click on New project select Windows >> Window Forms Application
Step 6: Right click on References>> click on Add Services References
Step 7: click on OK Button
Step 8: Write in Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowApplication.ServiceReference1;
namespace WindowApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
enum operation { Add = '+', Sub = '-', mul = '*', div = '/' };
private void txtSub_Click(object sender, EventArgs e)
{
Service2Client obj = new Service2Client();
Button btn=(Button)sender;
operation type=(operation)char.Parse(btn.Text);
switch (type)
{
case operation.Add:
txtResult.Text = Convert.ToString( obj.Add(Convert.ToDouble(txtFNum.Text),Convert.ToDouble(txtSNum.Text)));
break;
case operation.div:
txtResult.Text = Convert.ToString(obj.Div(Convert.ToDouble(txtFNum.Text), Convert.ToDouble(txtSNum.Text)));
break;
case operation.mul:
txtResult.Text = Convert.ToString(obj.Mul(Convert.ToDouble(txtFNum.Text), Convert.ToDouble(txtSNum.Text)));
break;
case operation.Sub:
txtResult.Text = Convert.ToString(obj.Sub(Convert.ToDouble(txtFNum.Text), Convert.ToDouble(txtSNum.Text)));
break;
}
}
}
}
Step 9: Build and run the project.