Thursday 23 September 2010

What are WCF behaviours?

WCF concepts such as endpoints, bindings, operations etc. have all seemed straight forward to me but for some reason behaviours have not. For example why, if I expose a metadata endpoint do I also have to explicitly implement a metadata behaviour? This post is an aide memoire about behaviours.

What are behaviours?

“Behaviors are types that modify or extend Service or Client functionality. For example, the metadata behavior that ServiceMetadataBehavior implemented controls whether the Service publishes metadata. Similarly, the security behavior controls impersonation and authorization, while the transactions behavior controls enlisting in and auto-completing transactions.

Behaviors also participate in the process of building the channel and can modify that channel based on user-specified settings and/or other aspects of the Service or Channel.

A Service Behavior is a type that implements IServiceBehavior and applies to Services. Similarly, a Channel Behavior is a type that implements IChannelBehavior and applies to Client Channels.” *

Michele Leroux Bustamonte, in her book Learning WCF describes behaviours in the following terms:

“While endpoints describe where to reach the service, which operations are available at the specified address, and what protocols are required – behaviors affect the service model locally at the client or service. What this means is that behaviours are not exposed as part of metadata, and they are not shared between clients and services. Instead, the locally affect how the service model processes messages.” **

There are 4 types of behaviour: service, endpoint, operation and contract. Endpoint behaviours can be associated with service endpoints or client endpoints.

A WCF behaviour can be added to the runtime using one of the following methods:

  • Programmatically
  • Using attributes
  • Through configuration

Service behaviours:

  • Implement System.ServiceModel.Description.IServiceBehavior
  • Applies to the service itself or to specific endpoints, contracts, and operations
  • Can be added by attribute, configuration or programmatically

Endpoint behaviours:

  • Implement System.ServiceModel.Description.IEndpointBehavior
  • Affects the operation of a particular endpoint
  • Can be added by configuration or programmatically

Contract and operation behaviours are used to apply extensions to contracts and operations. Contract behaviours:

  • Implement System.ServiceModel.Description.IContractBehavior
  • Applies to a particular contract
  • Can be added by attribute or programmatically

Operation behaviours:

  • Implement System.ServiceModel.Description.IOperationBehavior
  • Affects a particular operation
  • Can be added by attribute or programmatically

Endpoint, contract, and operation behaviours can be applied to both services and clients, but service behaviours can only be applied to services.

See also Extending WCF with Custom Behaviors.

* Windows Communication Foundation Architecture Overview
** Learning WCF

Thursday 23 September 2010