articles

Home / DeveloperSection / Articles / Calculator in WCF

Calculator in WCF

priyanka kushwaha4953 13-Mar-2015

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.

 

Calculator in WCF

Step 3Write 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]
    publicinterfaceIService1
    {
 
        //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.
    publicclassService1 : IService1
    {
 
        publicdouble Add(double firstNum, double secondNum)
        {
            return (firstNum + secondNum);
        }
        publicdouble Mul(double firstNum, double secondNum)
        {
            return (firstNum * secondNum);
        }
        publicdouble Sub(double firstNum, double secondNum)
        {
 
            return (firstNum - secondNum);
        }
 
        publicdouble 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

Calculator in WCF

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
{
   
    publicpartialclassForm1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        enumoperation { Add = '+', Sub = '-', mul = '*', div = '/' };
        privatevoid txtSub_Click(object sender, EventArgs e)
        {  
          
            Service2Client obj = newService2Client();
             Button btn=(Button)sender;
            operation type=(operation)char.Parse(btn.Text);
            switch (type)
            {
                caseoperation.Add:
                    txtResult.Text = Convert.ToString( obj.Add(Convert.ToDouble(txtFNum.Text),Convert.ToDouble(txtSNum.Text)));
                    break;
                caseoperation.div:
                    txtResult.Text = Convert.ToString(obj.Div(Convert.ToDouble(txtFNum.Text), Convert.ToDouble(txtSNum.Text)));
                    break;
 
                caseoperation.mul:
                    txtResult.Text = Convert.ToString(obj.Mul(Convert.ToDouble(txtFNum.Text), Convert.ToDouble(txtSNum.Text)));
                    break;
 
                caseoperation.Sub:
                    txtResult.Text = Convert.ToString(obj.Sub(Convert.ToDouble(txtFNum.Text), Convert.ToDouble(txtSNum.Text)));
                    break;
 
            }
          }
      }
 }

 

Step 9: Build and run the project.


            Calculator in WCF

 

 

 

 

 


Leave Comment

Comments

Liked By