Wednesday, May 30, 2012

WCF Service Creation With 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].
[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.

No comments:

Post a Comment