You are on page 1of 2

WCF Tutorial, Part 1: Introduction

by Behnam 01. December 2013 WCF 0


Overview
Prior to WCF, different communication technologies such as ASP.NET Web Services,
.NET Remoting, and ASP.NET s Web Service Enhancements (reliability, platform inde
pendent security, and atomic transactions) have been used. DCOM which is used by
.NET Enterprise Services with its automatic transaction support is even faster
than .NET Remoting.
Windows Communication Foundation (WCF) introduced by .NET 3.0 which includes all
the features from these predecessors and combines them into one programming mod
el.
WCF Offers:
Hosting for components and services
WCF can be hosted in the ASP.NET runtime, a
Windows service, a COM+ process, or a Windows Forms application.
Declarative behavior
Communication channels HTTP, TCP, IPC channels, or custom channels
Security infrastructure
WSE
Extensibility
Support of previous technologies
The goal is to send and receive messages between a client and a service. This sh
ould be done in a platform-independent way. Typically a service offers an endpoi
nt that is described by a contract, a binding, and an address (ABC):
Contract: defines the operations offered by the service;
Binding: gives information about the protocol and encoding; and
Address: is the location where the service can be accessed.
The client needs a compatible endpoint to access the service.

Figure 1

Components Participate in WCF Communication

The client invokes a method on the proxy.


The proxy offers methods as defined by the service but converts the method call
to a message and transfers the message to the channel.
The channel has a client-side part and a server-side part that communicate acros
s a networking protocol.
From the channel, the message is passed to the dispatcher, which converts the me
ssage to a method call invoked with the service.
WCF supports several communication protocols:
SOAP, A platform-independent protocol that is the foundation of several web serv
ice specifications to support security, transactions, reliability.
Web Services Description Language (WSDL), Offers metadata to describe a service.
Representational State Transfer (REST), Used with RESTful Web services to commun
icate across HTTP.
JavaScript Object Notation (JSON), Enables easy use from within JavaScript clien
ts. .NET includes a data contract serializer to create objects with the JSON not
ation.
Contracts
A contract defines what functionality a service offers and what functionality ca
n be used by the client. The contract can be completely independent of the imple
mentation of the service. The contracts defined by WCF can be grouped into four
different contract types: Data, Service, Message, and Fault. The contracts can b
e specified by using .NET attributes:
Data contract, the data contract defines the data received by and returned from

the service. The classes used for sending and receiving messages have data contr
act attributes associated with them.
Service contract, the service contract is used to define the WSDL that describes
the service. This contract is defined with interfaces or classes.
Operation contract, the operation contract defines the operation of the service
and is defined within the service contract.
Message contract, if complete control over the SOAP message is needed, a message
contract can specify what data should go into the SOAP header and what belongs
in the SOAP body.
Fault contract, the fault contract defines the error messages that are sent to t
he client.
Service Behavior
Specifies the internal execution behavior of a service contract implementation.
Apply the ServiceBehaviorAttribute attribute to a service implementation to spec
ify service-wide execution behavior. (To specify execution behavior at the metho
d level, use the OperationBehaviorAttribute attribute.) This attribute can be ap
plied only to service implementations.
Binding
Bindings are objects that are used to specify the communication details that are
required to connect to the endpoint of a Windows Communication Foundation (WCF)
service. Each endpoint in a WCF service requires a binding to be well-specified
.
The information in a binding can be very basic, or very complex. The most basic
binding specifies only the transport protocol (such as HTTP) that must be used t
o connect to the endpoint. More generally, the information a binding contains ab
out how to connect to an endpoint falls into one of the following categories:
Determines the security mechanism being used: either reliable messagin
Protocols
g capability or transaction context flow settings.
Encoding Determines the message encoding (for example, text or binary).
Transport
Determines the underlying transport protocol to use (for example, TCP
or HTTP)
Depending on the binding, different features are supported. The bindings startin
g with WS are platform independent, supporting web services specifications. Bind
ings that start with the name Net use binary formatting for high-performance com
munication between .NET applications.

Hosting
WCF can be host in Windows service, a COM+ application, WAS (Windows Activation
Services) or IIS, a Windows application, or just a simple console application. W
hen creating a custom host with Windows Forms or WPF, you can easily create a pe
er-to-peer solution.
Clients
A client application needs a proxy to access a service. There are three ways to
create a proxy for the client:
Studio Add Service Reference, this utility creates a proxy class from the metada
ta of the service.
ServiceModel Metadata Utility tool (Svcutil.exe), you can create a proxy class w
ith the Svcutil utility. This utility reads metadata from the service to create
the proxy class.
ChannelFactory class, this class is used by the proxy generated from Svcutil; ho
wever, it can also be used to create a proxy programmatically.