2006-06-16
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 3 of 3 )
At the broadest level, a distributed application created with WCF is comprised of services and clients.
WCF Services
A WCF service is a program that exposes one or more endpoints. An endpoint is a gateway for communicating with the world, and it has three main parts:
1. An address, the network address where the endpoint is located.
1. A binding that specifies how the endpoint communicates. The binding specifies things such as the encoding (binary or text, for example), the transport protocol (TCP, HTTP, etc.), and the security measures in place (SSL or SOAP Message Security, for example).
3. A contract that specifies what precisely the endpoint does. The contract details the operation(s) that the service can carry out. Each operation is defined in terms of messages that the endpoint can receive and, in some cases, respond to.
In the WCF API, an endpoint is represented by the ServiceEndpoint class. Each instance of this class encapsulates EndpointAddress, Binding, and ContractDesciption objects that correspond to the three elements in the list above.
A WCF service is represented by the ServiceDescription class. An instance of this class will contain one or more ServiceEndpoint objects that represent the endpoint(s) that the service exposes.
WCF Clients
A WCF client is a program that communicates with one or more endpoints. In some situations, a client will expose one or more endpoints when the functionality of the application requires that the service be able to initiate communication with the client (this is called duplex messaging).
Code Samples
Let's look first at how a WCF service app would define and implement a contract. These code examples are not meant to be the only or even the best way to do things, but are included to show you how simple WCF makes things.
The first step is to define the contract with an interface. The [ServiceContract] annotation is what links this interface to the WCF API.
using System.ServiceModel;
//Define WCF contract with an interface
[ServiceContract]
public interface IText
{
[OperationContract]
string Combine(string a, string b);
}
To implement the contract you create a class that implements the interface:
public class TextService : IText
{
string Combine(string a, string b);
{ return a + b; }
}
So far, we have defined a contract. In effect, it says "send me two strings, and I'll combine them and return the result to you." The next step for the service program is to define an endpoint based on this contract and start the service running:
public class MyWCFServiceApp
{
public void DefineEndpoint()
{
ServiceHost sh = new ServiceHost(typeof(TextService));
sh.AddServiceEndpoint(
typeof(IText), // Specifies the contract type
new WSHttpBinding(), // Specifies the binding
"http://someserver/TextService/Ep1"); // Specifies the endpoint's address
sh.Open();
}
}
At the client end, things are also surprisingly simple. The bulk of the work is done by the svcutil.exe utility which generates a proxy class from the service's metadata. This proxy class implements the same contract — that is, interface — as the service itself:
public class TextProxy : IText
{....}
This proxy class contains all of that "plumbing" that does the actual work of sending a message to the service and receiving any reply. All the client program needs to do is call the endpoint, or method, just as if the service were local:
...
TextProxy myproxy = new TextProxy();
string result = myproxy.Combine("Micro", "soft");
...
Summary
This has been a very brief and superficial introduction to Windows Communication Foundation. If you have worked with Web services in .Net, some aspects of WCF will seem familiar to you. This new API will make the development of distributed applications a lot easier than before. Given that more and more of today's business needs are best served by the distributed model, this will be a welcome addition to any programmer's toolkit.
For further information on WinFX, WCF, and to download preview versions of the SDK, visit http://msdn.microsoft.com/winfx/technologies/communication/default.aspx.
![]() |
|


