Windows Communication Foundation (WCF) is Microsoft's programming model for
building service-oriented applications. WCF is a part of .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). 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.
Direct Hit! |
Applies To: .NET developers USP: Build a service oriented app using WCF Primary Link: msdn.microsoft.com Keywords: Windows Communication Foundation |
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
{
public interface IPCQuest
{
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;
}
Host and Run a Basic WCF service
In this section we will host and run WCF service. To achieve it, first create a
URI (Uniform Resource Identifier) instance for the base address of the service
(http://localhost:8000/ServiceModelSamples/Service) and then to host service use
the namespace 'System. ServiceModel.Description' and create 'ServiceHost'
instance. Finally add an endpoint that exposes the service and enables Metadata
Exchange. Following code snippet shows the same:
To use Service Model Metadata Utility Tool (SvcUtil.exe) open VS 2005 command prompt and move to the folder where SvcUtil.exe is placed. Make sure that service is running. |
using System.ServiceModel.Description;
class Program
{
static void Main(string<> args)
{
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
ServiceHost selfHost = new ServiceHost(typeof(PCQuestService), baseAddress);
try
{
selfHost.AddServiceEndpoint(
typeof(IPCQuest),
new WSHttpBinding(),
"PCQuestService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
} } } }
Create, Configure, and Run WCF Client
To use the service created in previous steps we need a client. To create client,
add new 'Console Application 'project to current project and then again add
reference to 'System.ServiceModel.dll'. Now run the service created in previous
steps and while that service is running use 'SvcUtil.exe' to automatically
create client code and configuration file. Service Model Metadata Utility Tool (SvcUtil.exe)
can be found at 'C:\Program Files \Microsoft SDKs\Windows\v6.0\Bin'. In case you
are not able to find this file, download 'Windows SDK' from MSDN. This is how we
used this tool:
To add 'sandeepProxy.cs' and 'sandeep.config' to created client app, right click on 'sandeep1client'. Click on Add>Existing Items. |
Svcutil.exe /language:cs /out:sandeepProxy.cs /config:sandeep.config
http://localhost:8000/ServiceModelSamples/service
Where '/language:cs' implies that we are using c# language,'/out' changes the
name of the generated client proxy, and '/config' changes the name of client
configuration file.This will create 'sandeepProxy.cs' and 'sandeep.config' files
that need to be added to client program. Right click client project (sandeep
1client) in object explorer and then click on 'Add' and then click on 'Existing
Items' and finally add these two files to client project.
Last step in this implementation is creating a client instance that can use
WCF service. To implement the same, create an 'EndpointAddress' instance for the
base address of the service and then create WCF client object. Following code
snippet shows client program:
To show how WCF works in this implementation client service send 'request' to the running WCF service, which in turn replies with a string 'WCF IS WORKING'. |
using System.ServiceModel;
namespace ServiceModelSamples
{ class Client
{
static void Main()
{
EndpointAddress epAddress = new EndpointAddress ("http:// localhost: 8000/ServiceModelSamples/Service/PCQuestService");
PCQuestClient client = new PCQuestClient(new WSHttpBinding(), epAddress);
string value1 = "request";
string result = client.fun(value1);
Console.WriteLine("fun({0}) = {1}", value1, result);
client.Close();
Console.WriteLine();
Console.WriteLine("Press
Console.ReadLine();
} } } }
We have finished creating a service and client that uses this service. To
show how it works, first run the service (sandeep1.exe, in our case) and while
this service is running run the client (sandeep1client.exe in our case). The
client uses operations of service and produces appropriate output. In this way
we were able to understand basic mechanism of WCF programming and steps required
to create a simple WCF service and client application.