Overview on WCF Service Architecture
This article will help you to understand WCF Service Architecture. It describes major components of WCF like Contracts and its different types, Policies and Binding, Service Runtime, Messaging, Activation and Hosting and how they work together.
WCF service is collection of Endpoints and each endpoint describes how service is going to be communicate with clients. Endpoint has Address which tells where the WCF service resides, Bindings describes which protocol will be used for communication and Contract which gives details about how data will be exchanged.
Components of WCF
ContractsBasically 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
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 requirements.For best possible result from WCF Architecture you should use proper policy specification and choose bindings. See how to choose which WCF binding
Service RuntimeIt 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 or programmatically.
- 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 clients.
- 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 or Single.
- 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.