Home > DeveloperSection > Articles > Creating and Accessing WCF Services

Creating and Accessing WCF Services


WCF WCF 
Ratings:
0 Comment(s)
 5099  View(s)
Rate this:

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 2010 and On the File menu, point to New and then click Project.

2.     In the New Project dialog box, expand the Visual C# node and click WCF, followed by WCF Service Library. Give Name to your Service and Click OK to open the project.

 

Creating and Accessing WCF Services

 

3.     Now, you will find two file in your project Solution Explorer, named IService1.vb and IService1.cs.

Creating and Accessing WCF Services

 

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

 

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

 

3.     In the Request box :

·         Select the Value field of Operation 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 dialog box is displayed, click OK. The result will be displayed in the Response box.

 

Creating and Accessing WCF Services

 

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. Click OK to open the project.

3.     Right-click WindowsApplication1and click Add Service 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

Testing the Service (To build a client application)

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

Creating and Accessing WCF Services

 

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


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

Follow MindStick