Below diagram from MSDN describes you how WCF application works.
Components of WCF
Basically WCF contracts defines how service behaves and what opeartions it exposes
to clients. If service execution get some exception then service might issue fault
and contract decides what kind of fault to issue. Contracts also defines
What kind of MEPs required by service?
Below are WCF Contract Types
- Service Contract: defines which operations service makes avilable for communication
with clients and implications for the underlying message exchange while communication.
Service Contract is collective mechanisms by which a service's capabilities and
requirement are describes to clients. You create a WCF Service Contract by defining
.NET interface. See more details on Service Contract Attribute
- Operation Contract: is a part of WCF Service Contract and can be applied
only to methods. WCF uses opt-in model to define what can be exposed to clients
means you have to explicitly apply [OperationContract] to methods. It is like web
methods in ASMX web service. See more on Operation Contract Attribute
- Data Contract: defines structure of data included in SOAP messages while
communication between service and clients. It creates a custom data type which represents
business entity like Customer, Product etc and it becomes a formal agreement between
Service and Client which describes what kind of data to be exchanged. WCF uses Data
Contract Serializer by default to serialize and deserialize data. See how to implement DataContract in WCF?
- Message Contract: allows you to manage how messages to be structured and
control message headers. It gives you nice platform to control WCF messages. Some
time the default soap messages generated by WCF are not capable to fulfill your
requirements and you wish to customize or modify it using MessageContracts. See
implementing WCF MessageContract
Policies and Binding
It specifies required or optional that client should follow for communication with
WCF service. Policies are defined in WS-Policy specification. For example if WCF
service defines a endpoint which supports transport security, reliable messaging
and a binary encoding and other specifications, this endpoint details will be exposed
by WSDL to client as policy assertion. Client will understand it and follow those
For best possible result from WCF Architecture you should use proper policy specification
and choose bindings. See how to choose which WCF binding
It describes different behavior that can make effect on service run time. After
you design and implement ServiceContracts you can configure service and operation
behavior. Using those behaviors you can achieve through puts, reliability and performance
of services. Below are WCF behaviors which can be configured in configuration file
- Throttling Behavior: ServiceThrottlingBehavior class exposes properties like
MaxConcurrentCalls, MaxConcurrentInstances, MaxConcurrentSessions. These properties
are very useful for WCF services perfromance. Using this you can decide how many
instances or sessions can be created at application level
- Error Behavior: It allows you to decide how to handle errors or exceptions
occured at WCF layer. FaultContract are widely used to handle WCF exceptions.
- Metadata Behavior: specify whether service metadata is avilable to clients.
By adding mex endpoint and configuring or adding programmatically serviceMetadata
element of behavior element in config file you can enable or disable metadata to
- Instance Behavior: decides how objects are created and refers to the life
time of the service object. Whenever clients make request runtime will create service
objects to provide the response. Through instancing we can control how long this
service instance wants to be retained. By setting InstanceContextMode property of
ServiceBehavior you can control instance behavior. You can set it as PerCall, PerSession
- Transaction Behavior: enables WCF operations to be complies with ACID (Atomic,
Consistency, Isolation, Durability) properties and execute operations as single
logical unit where either all or none of the statements get executed. Transctions
work with WCF operations however it also depend on how you configure your service
and what binding you use.
- Dispatch Behavior: controls how a message is processed to and from WCF Infrastructure.
You can use message compression and optimized file type encoding.
WCF messaging provides a unified programing model and flexibility for presenting
data and messages exchanges. Messaging layer has many channels, those channels are
called as Channel Stack. Each channel involves in some or other way in processing
of message like authentication, sending or recieving messages to and from endpoint.
Channels are included in System.ServiceModel.Channels namespace.
There are two types of channels
- Protocol Channels: are Transaction Protocol, Reliable Messaging Protocol
and Security Protocol. Implements SOAP based protocol by processing and possibly
modifying message. E.g. WS-Security and WS-Reliability.
- Transport Channels: includes Message Encoding and Transport Protocol. Handles
sending and receiving message from network.
Activation and Hosting:
WCF service can be host in multiple ways and activate for clients. Every method
for hosting comes with advantages and disadvantages. While choosing hosting environment
for your WCF service you should consider - what protocols are used, what kind of
security you required, how service should be activated for clients.
Below are different ways to host your WCF service.
- IIS: you can host only HTTP/HTTPs based WCF services in if your using IIS
6.0 or previous versions. Hosting in IIS gives feature like "activation on demand"
your service code is not in memory for all time and gets instantiated whenever first
request comes in.
See how to
host WCF service in IIS
- WAS: is the new process activation method that comes with IIS 7.0. You can
host WCF service and have HTTP and non HTTP (TCP or netNamedPipe protocols) based
message based activation
- Self-Hosting: WCF service can be self hosted as console application, Win
Forms or WPF application with graphical UI. Self-hosting might look like a lot of
work - but it does give you the best flexibility: you can pick your protocols as
you like, you can set up your own addressing scheme the way you like it, and you
have total control over what gets done when. You can introduce your own custom ServiceHost
if you need to do some extra work to host services, and so on.
- Windows Service: WCF service objects lifetime will be controlled by Service
Controller Manager(SCM) of Windows. SCM is avilable for all versions of Windows.
Advantages of hosting WCF service in Windows Service includes service remains activated
and avilable for clients as there is no runtime activation, any type of protocols
can be used, more security through Windows Identity.
See hosting WCF service in Windows service