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,

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. namespace RKFirstWCFService
  9. {
  10. [ServiceContract]
  11. publicinterfaceIService1
  12. {
  13. [OperationContract]
  14. string FindCaller(myDataContract myDataCont);
  15. }
  16. [DataContract]
  17. publicclassmyDataContract
  18. {
  19. double phNo;
  20. [DataMember]
  21. publicdouble PhoneNumber
  22. {
  23. get {
  24. return phNo;
  25. }
  26. set {
  27. phNo = value;
  28. }
  29. }
  30. }
  31. }
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. namespace RKFirstWCFService
  9. {
  10. publicclassService1: IService1
  11. {
  12. publicstring FindCaller(myDataContract myDataCont)
  13. {
  14. // You can write your own logic here to find the caller
  15. if (myDataCont.PhoneNumber == 9848098480) {
  16. return "Ramakrishna Basagalla";
  17. } else {
  18. return "Record Not Found";
  19. }
  20. }
  21. }
  22. }
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. namespace RKWebServiceClient
  11. {
  12. publicpartialclassForm1: Form
  13. {
  14. public Form1() {
  15. InitializeComponent();
  16. }
  17. privatevoid button1_Click(object sender, EventArgs e)
  18. {
  19. // Create Proxy to consume the WCF Service
  20. RKWcfServiceReference.Service1Client rKProxy = new RKWcfServiceReference.Service1Client();
  21. // Create object for Data Contract to access the data members
  22. RKWcfServiceReference.myDataContract dcObj = new RKWcfServiceReference.myDataContract();
  23. dcObj.PhoneNumber = Convert.ToDouble(textBox1.Text);
  24. // Call the Wcf service
  25. MessageBox.Show(rKProxy.FindCaller(dcObj));
  26. }
  27. }
  28. }
Step: 9

Run and test the WCF Service.

Output:

Output

Output

Read more articles on WCF: