Creating WCF Service For Beginners

Introduction

This article will explain how to create and consume basic WCF service using Visual Studio 2012. At the end of the article you will able to create your first WCF Service.

What is WCF Service?

Windows Communication Foundation is a SDK for developing, configuring and deploying the services in windows.

WCF service is based on SOAP and returns data in XML form. It is the evolution of the web service(ASMX) and supports various protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ. It is not open source but can be consumed by any client that understands xml. It can be hosted within the application or on IIS or using window service.

What is ABC?

ABC is the three building blocks of WCF and they are known as,
  • A - Address (Where): Address tells us where to find the services, like url.
  • B - Bindings (How): Bindings tells us how to find the services or using which protocols finds the services (SOAP, HTTP, TCT etc.),
  • C - Contacts (What): Contracts are an agreement between the consumer and the service providers that explains what parameters the service expects and what return values it gives.

Steps to create WCF Service

In this example I am going to create sample WCF service called RKFirstWCFService, this service has a function called FindCaller to find the caller name for given phone number.

Add another Windows application to consume this WCF Service.

Step: 1

Open Visual Studio as an Administrator.

Visual Studio

Step: 2

Create WCF Service Application,

 Create WCF Service

Stop: 3

Remove the existing Service contracts and Data contracts and add below ones.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8.   
  9. namespace RKFirstWCFService  
  10. {  
  11.     [ServiceContract]  
  12.     publicinterfaceIService1  
  13.     {  
  14.         [OperationContract]  
  15.         string FindCaller(myDataContract myDataCont);  
  16.     }  
  17.   
  18.     [DataContract]  
  19.     publicclassmyDataContract   
  20.     {  
  21.         double phNo;  
  22.   
  23.         [DataMember]  
  24.         publicdouble PhoneNumber  
  25.         {  
  26.             get {  
  27.                 return phNo;  
  28.             }  
  29.             set {  
  30.                 phNo = value;  
  31.             }  
  32.         }  
  33.     }  
  34. }  
Step: 4

Implement IService1 methods in Service. Write your own code to display caller name based on the phone number.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8.   
  9. namespace RKFirstWCFService   
  10. {  
  11.     publicclassService1: IService1  
  12.     {  
  13.         publicstring FindCaller(myDataContract myDataCont)  
  14.         {  
  15.             // You can write your own logic here to find the caller  
  16.             if (myDataCont.PhoneNumber == 9848098480) {  
  17.                 return "Ramakrishna Basagalla";  
  18.             } else {  
  19.                 return "Record Not Found";  
  20.             }  
  21.         }  
  22.     }  
  23. }  
Step: 5

Edit the Web.Config and add Service and serviceBehaviors(copy and paste the below code)

config

Configuration code:
  1. <?xmlversion="1.0"?>  
  2.     <configuration>  
  3.         <appSettings>  
  4.             <addkey="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  
  5.         </appSettings>  
  6.         <system.web>  
  7.             <compilationdebug="true" targetFramework="4.5" />  
  8.             <httpRuntimetargetFramework="4.5" />  
  9.         </system.web>  
  10.         <system.serviceModel>  
  11.             <services>  
  12.                 <servicebehaviorConfiguration="NewBehavior0" name="RKFirstWCFService.Service1">  
  13.                     <endpointaddress="RKEndpoint" binding="basicHttpBinding" bindingConfiguration="" contract="RKFirstWCFService.IService1" />  
  14.                     </service>  
  15.             </services>  
  16.             <behaviors>  
  17.                 <serviceBehaviors>  
  18.                     <behaviorname="">  
  19.                         <serviceMetadatahttpGetEnabled="true" httpsGetEnabled="true" />  
  20.                         <serviceDebugincludeExceptionDetailInFaults="false" />  
  21.                         </behavior>  
  22.                         <behaviorname="NewBehavior0">  
  23.                             <serviceMetadatahttpGetEnabled="true" />  
  24.                             </behavior>  
  25.                 </serviceBehaviors>  
  26.             </behaviors>  
  27.             <protocolMapping>  
  28.                 <addbinding="basicHttpsBinding" scheme="https" />  
  29.             </protocolMapping>  
  30.             <serviceHostingEnvironmentaspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
  31.         </system.serviceModel>  
  32.         <system.webServer>  
  33.             <modulesrunAllManagedModulesForAllRequests="true" />  
  34.             <!--  
  35. To browse web app root directory during debugging, set the value below to true.  
  36. Set to false before deployment to avoid disclosing web app folder information.  
  37. -->  
  38.             <directoryBrowseenabled="true" />  
  39.         </system.webServer>  
  40.     </configuration>  
Step: 6

Add Windows application to consume the WCF Service.

Add

Step: 7

Add service reference and select WCF by discovering the WCF Service.

Note: To add the WCF service, the service must be in running.

reference

reference

Step: 8

Design the page (Add controls to page)

page

Code to access the WCF Service:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace RKWebServiceClient  
  12. {  
  13.     publicpartialclassForm1: Form   
  14.     {  
  15.         public Form1() {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         privatevoid button1_Click(object sender, EventArgs e)   
  20.         {  
  21.             // Create Proxy to consume the WCF Service  
  22.             RKWcfServiceReference.Service1Client rKProxy = new RKWcfServiceReference.Service1Client();  
  23.   
  24.             // Create object for Data Contract to access the data members  
  25.             RKWcfServiceReference.myDataContract dcObj = new RKWcfServiceReference.myDataContract();  
  26.             dcObj.PhoneNumber = Convert.ToDouble(textBox1.Text);  
  27.   
  28.             // Call the Wcf service  
  29.             MessageBox.Show(rKProxy.FindCaller(dcObj));  
  30.         }  
  31.     }  
  32. }  
Step: 9

Run and test the WCF Service.

Output:

 

Output

Output

Read more articles on WCF:


Similar Articles