Contracts in WCF

Contracts

  • A contract defines certain aspects of the service such as format & structure of the message etc.
  • A contract is an agreement between client & service
  • Contract describes the data, punitions their return types, parameters, etc to be communicated with the service

 

Types of contracts in WCF

WCF supports 5 types of contracts

  • Data contracts
  • Operation contracts
  • Message contracts
  • Fault contracts
  • Service contracts

 

Data contract

The data type that a service should be use in metadata to enable others  to interoperate with the service/ described as data contract.

  • In general, these data contracts are created with in the data contract class by preceding with [data contract] attribute.
  • The descriptions of the data types are known as the data contract.
  • The types used / described with in the data contract can be used in any part of the messages as parameters / return types tec.
  • If the service is using only simple types it is not required to create data contract separately.

 

Operation contract

An operation contract defines the operations, their parameters & the return types.

  • All operations should be preceded with operation contract attribute that are to be used outside the WCF service
  • In WCF services the operations can be modeled as taking a single message & returning a single message.
  • Message contract:-
  • A message contract defines / describes the format of a message.
  • Message contract declares whether message elements should go in headers / with in the body etc
  • Message contract also describes that what level of security should be applied to which elements of the message.

 

Fault contract

A fault contract can be associated with service operation to denote errors that can be return

  • An operation can have zero% more fault contracts associated with it.
  • These errors are soap faults that are modeled as exceptions in the programming model

 

Service contract

A service contract can be said as the combination of other contracts mentioned above.

  • The service contract bundles together multi related operations into a single functional unit.
  • A service contract contains internally data contracts, operation contracts, message contracts & fault contracts.
  • The service contract can define the settings to be followed at service level
  • In most of the cases, the service contract is defined by creating an interface & preceding it with service contract attribute.
  • The actual service contract code is resulted by implementing that interface.
  • The service contract outlines wheel functionality that end point exposes to the client

 

Endpoint

An end point is a construct, at which messages are sent/received.

  • An endpoint contains a location i.e., an address., a specification of the communication mechanism i.e., binding & definition for set of messages that can be sent / received i.e., contract.
  • So, endpoint can be set as combination of ABC’s
  • All communication with in the WCF services will occur through the endpoints of the service.
  • End points will provide the clients complete access to the functionality that WCF service offers
  • A WCF service is exposed to it’s clients as a collection of endpoints.
  • A WCF service should contain minimum one endpoint.

 

Structure of a service with endpoints

Screenshot_70

 

 

An endpoint is defined in a web.confing file by using the 3 attributes address, binding & contract like

<endpoint address = “Value” binding = “Value” contract = “Value”>

</enclpoint>

 

 

Message & Message Patterns

A message is a self contained unit of data which contains several parts like body, header, etc.

Message are sent between endpoints of dient & service.

WCF supports following message patterns.

  1. Simple
  2. Duplex
  3. Request – reply

Simplex

This is one way communication from client to service.

In this mechanism / pattern client serds request / message to the service, service consumes that request but doesn’t respond back to the client.

To work / implement this message pattern set “ Is one way” property to “true” along with [operation contract] attribute like,

[operation contract (is one way = true)]

The methods which are following simplex message pattern should not return a value i.e., should not have return type (or) should not contain reference & out parameters. Otherwise, they through an conception “System invalid operation” exception.

 

Screenshot_71

 

 

Duplex

This is a two way / bidirectional communicate service and service respond to the client by conserve that message.

Client & service will exchange the messages cuing channel

A duplex service contract is a message exchange pull in which both client & service endpoints can semi messages to each other independently.

To implement this message pattern set “I someway property of [operation contract] attribute to “true” create the call back interface that defines the of operations for invoking on the client

 

 

Screenshot_72

 

 

Request – Reply

This is the default messaging path followed in WCF.

In this type of pattern client makes a call to service tions & waits for a response from the service

Client request & service response will be through event channels.

 

Screenshot_73