.NET framework 3.0 and it unifies the different communication models supported in .NET 2.0 like Web Services, .NET Remoting, Distributed Transactions, and Message Queues. To properly understand WCF we will first focus on the term Service Oriented Architecture (SOA).
|
Direct Hit!
|
Applies To: .NET developers
USP: Build a service oriented app using WCF
Primary Link: msdn.microsoft.com Keywords: Windows Communication Foundation |
In modern era there is clear shift in the field of development from localized systems toward distributed systems. In a distributed system, each part of the system is hosted at a different place and provides some services. As these distributed systems became common (thanks to ever-increasing functionality and acceptance of Web services) the SOA concept came into picture where a system is divided into parts that run as services.
In service oriented development, focus is on systems that constitute a number of autonomous services as compared to well-known object oriented development where focus is on application that is built from class libraries. In service orientation, one can use different technologies to develop a service and then use a common messaging system that can communicate between different parts of the system.
Consider an example of a company that needs to develop an application. Now business logic of this application needs to be accessed from other software of the company. One way of achieving this is to create new applications as service oriented that exposes logic via a set of well-defined services.
In this article we would be focusing on the basic concept of WCF programming. Here we will show how to create a simple WCF service and a client application that uses this service. We are going to use Visual Studio 2005 with c# as programming language to implement WCF service and client application.
Implementation
We will start by creating a service that exposes endpoints; endpoints contain the address where the service can be found, the information that a client must communicate with the service, and contract that defines functionality provided by the service to clients. Then we will create a client application that will use the service. Following are the steps required for implementation:
 |
|
To add 'System.ServiceModel.dll', right click the reference under 'sandeep1' and then click on Add reference move to the 'Recent' tab. Select the reference and click on 'Ok'.
|
Define and implement WCF service contract
A service contract defines what operations will be supported by the service. To implement it create 'Console Application' project in Visual Studio 2005 (we are using c#) add reference to 'System.ServiceModel.dll' to project. One can find this file at c:\Windows\Microsoft.Net\Framework\v3.0\Windows Communication Foundation. In program add 'System.ServiceModel' namespace and then define interface ('IPCQuest' in our case). After defining interface one has to declare method for the operation, 'IPCQuest' contract exposes. Following code snippet shows creation of contract in WCF:
using System.ServiceModel;
namespace Microsoft.ServiceModel.Samples
{
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface IPCQuest
{
[OperationContract]
string fun(string n1);
} }
Now that we have defined the contract, next step is to implement it. This is done by creating a class 'PCQuestService.' This class implements 'IPCQuest' interface as shown:
public class PCQuestService : IPCQuest
{
public string fun(string n1)
{
string result = "WCF IS WORKING";
Console.WriteLine("Received fun({0})", n1);
Console.WriteLine("Return: {0}", result);
return result;
}