Click here to Skip to main content
15,669,526 members
Articles / WCF
Technical Blog
Posted 11 Jun 2012

Tagged as


17 bookmarked

WCF Service Creation With C#

Rate me:
Please Sign up or sign in to vote.
4.80/5 (17 votes)
12 Jun 2012CPOL3 min read
Creating a WCF service in C#.

Basic WCF Service Creation in C#.Net => Part1

Hello all, this is the start up guide to create WCF service. This article is a pictorial step by step help to create WCF application.

WCF (Windows Communication Foundation) is a part of .NET 3.0. So you need to install Visual Studio 2008 to use WCF.

It is platform for SOA. WCF provides a way to create loosely coupled application.

WCF is intended to provide services that are distributed and interoperable.

It unifies ASMX and .NET Remoting, MSMQ and can be easily extended to support new protocol.

When we create a Service, to expose the functionality to the world, it needs to be hosted into Host Process. Service Host will be used to expose the end point of the service to other Service or Clients.

Let’s start with the steps...

We will start with VS 2008. Create a File -> New Project -> WCF ->WCFClassLibrary project as shown below

Select WCF in left panel and WCF Service Library in the right panel.

Give appropriate name in the Name Box. Click Ok.

The Project will contain a sample Service1.cs & IService.cs files. Delete it. So that we can add our own service and understand the things in better way.

Right click on MathsLibrary -> Add -> New Item -> Select Class1.cs. 

Rename it to MathsOperations.cs

This will create a simple class file. Open the file. Make the class public.

Similary add one more class IMathsOperations.cs, which will be an interface, which will provide a list of what all operations a WCF service will perform.

Open IMathsOperations.cs and change it to public interface IMathsOperations

And add the below code to it..

To make IMathsOperations as WCF Service Contract, add an attribute [ServiceContract]to it.

Also what all operation you want to make visible to client should be decorated with [OperationContract].<o:p>

[ServiceContract] and [OperationContract]are included in System.ServiceModel namespace.

Now once decided with the contract, we can implement this interface into our srevice as below

Build the project once you are done up to this.

Let us add app.config to Host solution.

App.config contains endpoint details, which includes ABC (Address, Binding and Contract)

Address is the Address WHERE the service can be found.

Binding the HOW to access the service

Contract is WHAT the service contains.

Now let’s modify App.config. Right click on App.config, click on Edit WCF Configuration

Below popup should appear

Select the Service1 from left panel, following window should show up

Click on ellipses button and go the path where the MathsLibrary.dll exists.

Click on it, it will give you the name of the service it contains.

Similarly go to Endpoints, and select proper contract with same steps as above. 

Also we can change the binding to be used as below…

Select Host on left hand side and it will show you base address, edit the base address to whatever you want..

Once this is done. Build the project.

VS 2008 provides you the way to host the service. We can also host it in console application or Windows Service or IIS.

We will learn those in next parts. Here we will use the host provided by VS 2008.

Now let’s create a client application. Add console application (Or any other project type) project in same solution or different solution.

Add service reference to the MathsService that we have created. It will automatically load all necessary dlls.

Then with the below code we can give a call to the service..

This will give you below output

In the next sub-sections we will see how to host this service in IIS, Windows services, Console applications etc.


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

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

Comments and Discussions

QuestionGreat Pin
mtoha9-Nov-14 16:15
professionalmtoha9-Nov-14 16:15 
Nice tutorial, I make my first WCF application and how to consume it.
Smile | :)
Questionuse wcf Pin
vajihe.mirzaei5-Jun-14 9:27
vajihe.mirzaei5-Jun-14 9:27 
QuestionMy service is not visible in my solution Pin
Dinesh9213-Dec-13 18:29
professionalDinesh9213-Dec-13 18:29 
Suggestionthere exists a well written documentation Pin
VallarasuS12-Jun-12 20:49
VallarasuS12-Jun-12 20:49 
SuggestionBlog Link Pin
Tim Corey11-Jun-12 4:39
professionalTim Corey11-Jun-12 4:39 
QuestionPlagiarism is never a good idea...[WITHDRAWN] Pin
OriginalGriff11-Jun-12 3:38
mvaOriginalGriff11-Jun-12 3:38 
AnswerRe: Plagiarism is never a good idea... Pin
Tim Corey11-Jun-12 4:38
professionalTim Corey11-Jun-12 4:38 
GeneralRe: Plagiarism is never a good idea... Pin
OriginalGriff11-Jun-12 4:52
mvaOriginalGriff11-Jun-12 4:52 
GeneralImage formatting at the end. Pin
Ammar_Ahmad11-Jun-12 2:53
Ammar_Ahmad11-Jun-12 2:53 

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.