Windows Communication Foundation - SPLessons

WCF Creating WCF Service

Home > Lesson > Chapter 8
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

WCF Creating WCF Service

Creating WCF Service

shape Description

This lesson describes the first WCF Service in the simplest steps. The following is the procedure.

shape Conceptual figure

shape Step-1

Install any edition of visual studio.
  • Open Visual Studio
  • Click on File -> New -> Project
  • In Visual C# tab -> select WCF option.
 

shape Step-2

After creating the project, It generates two files those are IService.cs, and Service.cs which are optional. Delete those files if not needed. Every WCF Service Contains one or more Interfaces and Implemented Classes.Here is an example for creating a WCF Service. [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfTestClient { // NOTE: You can use the "Rename" command on the "Refactor" menu tochange the interface name "IService1" in both code and config file together. [ServiceContract] public interface IService1 { [OperationContract] int add(int value1, int value2); [OperationContract] int Divide(int num1, int num2); } } [/csharp] The code behind its class is given below. [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfTestClient { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. public class Service1 : IService1 { public int add(int value1,int value2) { return value1 + value2; } public int Divide(int num1, int num2) { if (num2 != 0) { return (num1 / num2); } else { return 1; } } } } [/csharp] To run the above service, Click the Start button in Visual Studio. The below diagram shows how to run the application.   When the application is running, below screens appears.  

shape Step-3

Click the add() method, the following page opens. Here, one can enter any two integer numbers and click on the Invoke button. The service will return the summation of those two numbers.

shape Step-4

The following page appears on clicking the Divide() Method. Enter the integer numbers, click the Invoke button, and get the output as shown here: Once created a service, We can switch between them directly. It is shown in below figure.