Click here to Skip to main content
15,886,519 members
Articles / Programming Languages / C#
Tip/Trick

Why We Use WCF Service and Sample of WCF Service

Rate me:
Please Sign up or sign in to vote.
4.54/5 (17 votes)
5 Sep 2014CPOL3 min read 67.4K   459   17   13
Why we use WCF service and a sample of WCF service

Introduction

What is WCF Service?

WCF stands for Windows Communication Foundation and is part of .NET 3.0. WCF is a Microsoft platform for building distributed and interoperable applications.

WCF supports multiple languages and multiple platforms.

Why Should We Use WCF Service?

Let’s take these two scenarios:

We have two clients and we need to implement a service for them:

  1. The first client is using Java Application to interact with our service. So for interoperability, this client wants messages in XML format and the protocol to be HTTP.
  2. The second client uses .NET, so for better performance, this client wants messages in binary format and the protocol to be TCP.

Without WCF Service

  1. To satisfy the first client requirement, we use web service.

    Image 1

  2. To satisfy the second client requirement, we use .NET Remoting.

    Image 2

These are two different technologies, and have completely different programming models. So the developers have to learn different technologies.

So to unify and bring all technologies under one roof, Microsoft has come with a new programming model called WCF-Windows Communication Foundation.

With WCF

You implement one service and we can configure as many end points as we want to support all the client needs. To support the above 2 client requirements, we would configure 2 end points. In the endpoint configuration, we can specify the protocols and message formats that we want to use.

Image 3

So, the conclusion is that:

  1. A web service to exchange messages in XML format using HTTP protocol for interoperability
  2. A remoting service to exchange messages in binary format using TCP protocol for performance

Along the way, we will get a feel of how different these technologies are.

And last, we will discuss implementing a single WCF Service and configuring different ‘end points to support different transport protocols and message formats.

Now we create one WCF Calculator Service:

  1. Open your Visual Studio. File->new website->select Visual C#->select WCF service.

    Image 4

  2. This is default Iservice.cs.

    In which there are two attributes:

    1. ServiceContract: For Iservice interface and
    2. OperationContract: For methods, here it declares all methods which are accessible at client side

    Each method has its own OperationContract.

    Now, we create two methods for our calculator service.

    Image 5

  3. This is default Service.cs

    In which it implements Iservice interface and in this class, all the declared methods of Iservices.cs are defined.

    Image 6

  4. This is shown in the figure below:

    Image 7

  5. In Service.cs, right-click on IService and click on Implement Interface and select Implement Interface as in screen. Then, you can see whatever you have declared in Iservice is now implemented here and write your logic.

    Image 8

  6. Save your files and view in browser Services.cs.

    Image 9

  7. Your WCF service is running on the browser. Now copy that URL and later on at client side, it will be pasted.

    Image 10

  8. Now we create Client side app which uses WCF.

    Image 11

  9. Take two textboxes, one label and one button from the toolbox and drop in from1 as in screen.

    Image 12

  10. Right click on project and click on Add Service Reference.

    Image 13

  11. Paste that URL which is copied earlier, then click on Go. You can see your service with IService and your method comes, then click on Ok.

    Image 14

  12. Fire button click event of button which is in from1.Create object of WCF service and base on this object call method. Run the application and pass value. You can see the total is reflected at label1.

    Image 15

  13. When you run this application, you will get an output like this:

    Image 16

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow can we host this type of project because linking from localhost Pin
Member 108390892-Feb-22 0:37
Member 108390892-Feb-22 0:37 
Questiongood article Pin
Devyani Gami9-Nov-17 2:22
Devyani Gami9-Nov-17 2:22 
QuestionVery Best.. Pin
Member 1104237311-Sep-16 22:23
Member 1104237311-Sep-16 22:23 
GeneralMy vote of 5 Pin
Purushotham Agaraharam9-Mar-16 23:04
Purushotham Agaraharam9-Mar-16 23:04 
GeneralNice Explanation of Why WCF service used Pin
Pradip Jadhav, India8-Oct-15 19:50
Pradip Jadhav, India8-Oct-15 19:50 
QuestionIs WCF still used widely. Pin
george00084-Feb-15 5:43
george00084-Feb-15 5:43 
AnswerRe: Is WCF still used widely. Pin
Mehta Priya16-Jun-15 23:22
Mehta Priya16-Jun-15 23:22 
QuestionCompete 5 Pin
george00084-Feb-15 5:41
george00084-Feb-15 5:41 
SuggestionGood article - But: Pin
kris.allberry8-Sep-14 22:22
kris.allberry8-Sep-14 22:22 
GeneralGood Explanation Pin
Bheesham Kumar Sharma8-Sep-14 19:52
Bheesham Kumar Sharma8-Sep-14 19:52 
GeneralMy vote of 5 Pin
Dominic Burford8-Sep-14 1:29
professionalDominic Burford8-Sep-14 1:29 
GeneralMy vote of 4 Pin
cjb1107-Sep-14 20:46
cjb1107-Sep-14 20:46 
GeneralMy vote of 5 Pin
Enrique J. Gonzalez Fernandez5-Sep-14 7:47
Enrique J. Gonzalez Fernandez5-Sep-14 7:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.