Basics of WCF - Contracts


Windows Communication Foundation (WCF) application is divided two layers namely Services and Clients

The WCF services can be exposed in different ways through Web, Windows Service, Self Hosting [Running Console Application]. The Endpoint [Which is having the Address, Binding, Contract(ABC)] is definded to expose service or consume it.

In our sample code so far we have developed Console Based application for both Service and Client Applications; then we have deployed the serive in IIS 6.0 in Windows Server 2003 in the first set of applications. The next set of applications are Web Applications.

For developing a WCF Service we have gone through steps
A. Sevice Layer
Step I - Designing Contracts
Step II - Service Hosting and Selecting Bindings
Step III - Configuring for Hosting the Service

B. Client Application
Step IV - Based on Sevice creating Proxy for invoking the Service

Now we will discuss futher in details.

We have looked that in both application, we have the one common thing, that is Endpoint includes the Service Contact, Address and Binding; only service hosting and client applications are different. The service contact is first item to be designed fo a Service.

A service contract is all about:

  • Grouping of operations
  • Signature of the operations for exchanging Mesages
  • Data types of these messages.
  • Protocols and serialization formats for communicating the messages

The contact is a set of specific messages organized into basic message exchange patterns (MEPs), such as request/reply, one-way, and duplex

One Way - Datagram-style delivery
Request-Reply - Immediate Reply on same logical thread
Duplex - Reply later and on backchannel (callback-style)

Three Types of Contracts
Service Contract - Operations, Behaviors and Communication Shape
Data Contract - Defines Schema and Versioning Strategies
Message Contract - Allows defining application-specific headers and unwrapped body content

All the contracts are defined on .NET application as CLR types and and on the wire it represents as XML format - WSDL/XSD/SOAP. This is implemented through Attributes. Here the details for all the types

Service
The Service and operations defines in a service through ServiceContract and OperationContract attrubutes.
Mapping: CLR types -> Web Services Description Language (WSDL)

Data
Describes a data structure using DataContract and DataMember attributes.
Mapping: CLR types -> XML Schema Definition (XSD)

Message
Defines the structure of the message on the wire using MessageContract, MessageHeader, MessageBody
Mapping: CLR types -> Simple Object Access Protocol (SOAP) messages.

Fault/Exception
For any CLR exceptions defined as fault contract using FaultContract attribute and the fault's CLR Type converts to SOAP faults.
Mapping: CLR types -> SOAP faults

0 comments: