WCF – next generation platform for distributed applications

Service based applications changed the development practices. Service oriented architecture provided a new way to look at the application development and took it one step ahead in development world.

Web service and Simple Object Access Protocol (SOAP) has made data transmission between different applications very easy without worrying about underlying architecture and platform detail. SOAP architecture provides good platform for secured data transmission over HTTP with that said, if you have to develop an application that is going to run over TCP then you would have to determine and develop application that is going to support that kind of network.

Windows Communication Foundation (WCF) supports various protocols like TCP/IP, HTTP and even Peer networking using mesh network topology. In other words, WCF contains everything that modern service oriented application need.

Lets start with some basic ABC of WCF:

ABC of WCF: Address, Binding, Contract.

Address: is the location. It defines endpoint where the message should be sent by the client like:http://www.example.com/service1/

Binding: is channel. It defines the way by which messages will pass over the network like: TCP,HTTP or Named Pipes etc.

Contract: is methods or features. It defines what all methods or functions are available for client to use. It also defines message format.

Lets build our first WCF application:

1) Create new project in Visual Studio 2008 by selecting: File->New->Project -> Visual C# -> WCF -> WCF Service Library.

2) Visual Studio will create a number of files for you automatically, at this point as we are building our first application so we will not go into the detail of those files but there are two files we are interested in: IService1.cs and Service1.cs as shown:

 


3) IService1.cs is the interface that will define method that will be accessible to the client.

4) Replace your interface IService1 with the code below:

[ServiceContract]
public interface IService1
{
[OperationContract]
string GetProduct(int productID);
}


4) Now its time to implement our interface. I have created a conditional branch to return results in my code.

5) Save all changes in IService1.cs and open Service1.cs to implement the interface. Replace code of Service1.cs from the code below:

public class Service1 : IService1
{
private string result;

#region IService1 Members

public string GetProduct(int productID)
{

switch (productID)
{
case 1:
result= string.Format("Chai has ProductID: {0}", productID);
break;

case 2:
result = string.Format("Coffee has ProductID: {0}", productID);
break;

default:
result = string.Format("Invalid entry: {0}", productID);
break;
}
return result;
}

#endregion
}

6) Now its time to run the application. Run your application this will bring up WCF Test Client which is an internal hosting environment from VS2008.

7) Double click on your method "GetProducts()". In the request section in the value field enter "1″ and click "invoke". This will invoke the service and result will returned back in response section as shown:


Congratulations!!! you have just created your very first WCF based application.

In the next article we will explore WCF in more detail. Till than stay tuned and Happy Coding!!!


Tags: , , , ,

WCF