---
title: "Creating and Accessing WCF Services"  
description: "Following steps will show you to create a WCF Service and access them thorough the Client Application."  
author: "Dev Vrat Sahu"  
published: 2012-08-29  
updated: 2021-01-20  
canonical: https://www.mindstick.com/articles/986/creating-and-accessing-wcf-services  
category: "wcf"  
tags: ["wcf"]  
reading_time: 3 minutes  

---

# Creating and Accessing WCF Services

Following steps will show you to create a WCF Service and access them thorough the Client Application.\

##### To create WCF Service

**1.** Open [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) 2010 and On the File menu, point to New and then click Project.

**2.** In the New Project [dialog box](https://www.mindstick.com/blog/157/dialog-box-in-dot-net), expand the Visual C# node and click WCF, followed by WCF Service Library. Give Name to [your Service](https://answers.mindstick.com/qa/96974/what-to-do-if-you-want-to-upgrade-downgrade-or-cancel-your-service-plan) and Click OK to open the project.

![Creating and Accessing WCF Services](https://www.mindstick.com/mindstickarticle/1e59fe50-abd6-486f-bbb1-635516611129/images/aee84318-0f54-4b25-a27b-86b5fc83dbd3.png)

**3.** Now, you will find two file in [your project](https://www.mindstick.com/forum/156583/best-way-to-link-bootstrap-file-in-your-project) Solution Explorer, named IService1.vb and IService1.cs.

![Creating and Accessing WCF Services](https://www.mindstick.com/mindstickarticle/1e59fe50-abd6-486f-bbb1-635516611129/images/2fec77d3-46ab-4a73-af9f-02f30b171c74.png)

**4.** Double-Click on IService1.cs and write the following code:

```
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text; namespace WcfServiceLibrary1{      [ServiceContract]    public interface IService1 // Creating Interface    {        // TODO: Add your service operations here        [OperationContract]        double GetResult(string operation, double num1, double num2);              // when invoked, this function will parameters from Client Application        // and Return the Result of the Calculation.    }} 
```

**5.** Double-Click on Service1.cs and write following codes:

```
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text; namespace WcfServiceLibrary1{    public class Service1 : IService1 // inherriting the IService1 Interface declared in IService1.cs file    {              public double GetResult(string operation, double number1, double number2)        //this will accept three parameters and return the result of calculation        {            double result;            switch (operation)            {                case "Add":                    result = number1 + number2;                    break;                case "Subtract":                    result = number1 - number2;                    break;                case "Multiply":                    result = number1 * number2;                    break;                case "Divide":                    result = number1 / number2;                    break;                default:                    result = 0;                    break;            }            return result;         }          }}
```

\

##### Testing the Service

> To test a WCF Service

1. Press F5 to run the service. A WCF Test Client form will be displayed and it will load the service.\

![Creating and Accessing WCF Services](https://www.mindstick.com/mindstickarticle/1e59fe50-abd6-486f-bbb1-635516611129/images/6172f52d-1b0f-4a04-a61e-36fda347043c.png)

**2.** In the WCF Test Client form, double-click the GetResult() method under IService1. The GetResult tab will be displayed.

![Creating and Accessing WCF Services](https://www.mindstick.com/mindstickarticle/1e59fe50-abd6-486f-bbb1-635516611129/images/69aa28bb-c54f-4664-8f54-1a8d4312e8f4.png)

**3.** In the Request box :

· Select the Value field of [Operation](https://yourviews.mindstick.com/view/83916/why-is-it-necessary-to-sterilize-operation-theater) and type “Add” or “Subtract” or “Multiply” or “Divide”.

· Select the Value field of Num1 and type any Number.

· Select the Value field of Num1 and type any Number

**4.** Click the Invoke button. If a [Security Warning](https://answers.mindstick.com/qa/96025/how-do-i-get-rid-of-a-fake-microsoft-security-warning) dialog box is displayed, click OK. The result will be displayed in the Response box.

![Creating and Accessing WCF Services](https://www.mindstick.com/mindstickarticle/1e59fe50-abd6-486f-bbb1-635516611129/images/442a36d4-c9a4-4fc2-865a-897b28c58981.png)

**5.** On the File menu, click Exit to close the test form.

## Accessing the Service

###

##### To reference a WCF service

1. On the File menu, point to Add and then click New Project. \
2. In the New Project dialog box, expand the Visual C# node and select Windows, and then select Windows [Forms Application](https://www.mindstick.com/forum/33816/how-to-use-chart-controls-in-windows-forms-application-in-dot-net). Click OK to open the project. \
3. Right-click WindowsApplication1and click Add Service [Reference](https://www.mindstick.com/forum/34619/call-by-value-and-call-by-reference). The Add Service Reference dialog box will appear. \
4. In the Add Service Reference dialog box, click Discover. Service1 will be displayed in the Services pane. \
5. Click OK to add the service reference.

#####

![Creating and Accessing WCF Services](https://www.mindstick.com/mindstickarticle/1e59fe50-abd6-486f-bbb1-635516611129/images/c1722bf7-3ccd-4ec7-b032-78495648b39a.png)

### Testing the Service (To build a client application)

**1.** Create the following UI to test the WCF Service.

![Creating and Accessing WCF Services](https://www.mindstick.com/mindstickarticle/1e59fe50-abd6-486f-bbb1-635516611129/images/3a9c9b87-4fa9-4a66-ad0c-cd41b5a8c351.png)

**2.** Now, Double-Click on the Get Result Button and Add the following code on Click-Event of the Get Result Button.

```
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 WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }         ServiceReference1.Service1Client client;        private void Form1_Load(object sender, EventArgs e)        {            string[] operations = { "Add","Subtract","Multiply","Divide"}; // Creating an Array for List the Operations            cbOperation.Items.AddRange(operations); // Adding the Item in the Combo Box through Array        }         private void btnGetResult_Click(object sender, EventArgs e)        {            client = new ServiceReference1.Service1Client();                       double returnValue;            returnValue = client.GetResult(cbOperation.SelectedItem.ToString(), Convert.ToInt32(txtNum1.Text), Convert.ToInt32(txtNum2.Text));            // Paasing the parameters in the GetResult metrhod of WCF Service            lblResult.Text ="Result is : " + returnValue.ToString();            // Printing the value of Calculation returned by WCF Service        }    }}
```

**3.** Enter the Required values in the Fields and Click on Get Result Button to get the Result.

##### Output Window

![Creating and Accessing WCF Services](https://www.mindstick.com/mindstickarticle/1e59fe50-abd6-486f-bbb1-635516611129/images/6e9bca73-8a23-48e1-bd5a-550eed3d2a7b.png)

\

---

Original Source: https://www.mindstick.com/articles/986/creating-and-accessing-wcf-services

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
