Home > DeveloperSection > Articles > Calculator Sample in WCF

Calculator Sample in WCF


Web Development Web Development 
Ratings:
2 Comment(s)
 11057  View(s)
Rate this:

Calculator Sample in WCF

Introduction:


This is a simple calculator that contains four basic asthmatic functions for common calculation. We will create a simple [service contract] ,which in turn contains four [operation Contract]of each calculator function. After completing WCF service successfully we will use it in Windows Application and in same way to Web application.

So let’s start the WCF service.

Step 1:Start Visual studio 2010 and click->File->new->Project

Calculator Sample in WCF

Step 2: This will open a new dialog box select->WCF->WCF service Application and give appropriate name to your service and Click on OK button. Calculator Sample in WCF

Step3: Now you have opened the solution explorer click on IService1.cs and delete all the code, what it contain by default. And write your own code as give below: Rename IService class as ICalculator. Calculator Sample in WCF

Step4: Write down the code for contracted methods and interface in the Calculator.svc service class. Calculator Sample in WCF

Step 6: Configure the endpoint of this service in the web.config file as.

Calculator Sample in WCF

Step 7: Build and run the service as in usual way as you does in other projects of Visual studio. The following output will be display.

Calculator Sample in WCF

If you reach at this output stage, it means you have created service successfully. Otherwise something is wrong in your project.

Now if you want to see the Meta data what was exchange by the service, the use red rounded lick to do so.

 Ok now we assume you have created the service successfully, so the next step is how take the service what was is created.

Communicating with WCF using WINDOWS Application

 

Step1: Take a simple window application and design the GUI as in given figure

Calculator Sample in WCF

Step:2: Add the service references in your project what was created by you above.Step2:  Add

Calculator Sample in WCF

Step3: Now you have opened a new dialog box, enter the service address and click on ok button.

Calculator Sample in WCF

Now you can see the service references are add in your solution explorer, now you can use all the services contracted under this service referenced and (depends on what endpoint defined in service)

Calculator Sample in WCF

Step4: Write down the following code in your program for consuming service provided by the WCF.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace ConsumeServiceCalcInWindows

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        // creating object(some times called Proxi) of service under ServiceReferences1

        ServiceReference1.CalculatorClient Math;

 

        private void Form1_Load(object sender, EventArgs e)

        {

            // initialisinty the object

            Math = new ServiceReference1.CalculatorClient();

        }

        private void btnAdd_Click(object sender, EventArgs e)

        {

            // using the operatiContrscted methos of service cotrcted in WCF of ICalculator which is defined in Calculator service

            txtResult.Text = Math.AddNumber(Convert.ToDouble(txtFirstNumber.Text), Convert.ToDouble(txtSecondNumber.Text)).ToString();

        }      

 

        private void btnSub_Click(object sender, EventArgs e)

        {

            // using the operatiContrscted methos of service cotrcted in WCF of ICalculator which is defined in Calculator service

            txtResult.Text = Math.SubNumber(Convert.ToDouble(txtFirstNumber.Text), Convert.ToDouble(txtSecondNumber.Text)).ToString();

        }

 

        private void btnMult_Click(object sender, EventArgs e)

        {

            // using the operatiContrscted methos of service cotrcted in WCF of ICalculator which is defined in Calculator service

            txtResult.Text = Math.MultNumber(Convert.ToDouble(txtFirstNumber.Text), Convert.ToDouble(txtSecondNumber.Text)).ToString();

        }

 

        private void btnDiv_Click(object sender, EventArgs e)

        {

            // using the operatiContrscted methos of service cotrcted in WCF of ICalculator which is defined in Calculator service

            txtResult.Text = Math.DivNumber(Convert.ToDouble(txtFirstNumber.Text), Convert.ToDouble(txtSecondNumber.Text)).ToString();

        }       

    }

}

 

Step5: Build and run the project, and see the output.

Calculator Sample in WCF

You run and test as you want. I hope you learn the basic programming with WCF and, how to use it in any application.

The same concept you can use to with web Application. So you should to try this sample in web Application by yourself.

Thanks.


Error

By Esra Ozbay on   2 years ago
I've done same project as you but I receive an error, I couldn't solve it. 
"NullReferenceException was unhandled
Object reference not set to an instance of an object."

good example

By Sunil Bhagwat on   2 years ago

Dear sumit

This is very good example only service name and contract in end point should be reversed in web config.

then only service is created

Sunil Bhagwat


Don't want to miss updates? Please click the below button!

Follow MindStick