WCF Behaviours

WCF has an extensible architecture that allows us to customise various elements like Hosts, Bindings, Channels and Behaviours. Behaviours enable us to modify the runtime behaviour of services and add custom extensions to inspect and validate the service configuration and messages.

Behaviours come in four flavours based on their scope so it is important to choose the right type:

Service Behaviour
- Applies to the entire service runtime (which may implement multiple Contracts), including ServiceHostBase.
- Implements IServiceBehavior.

Contract Behaviour

- Applies to the entire contract, ClientRunTime and DispatchRuntime.
- Implements IContractBehavior.

Operation Behaviour

- Applies to the service operation, ClientOperation and DispatchOperation.
- Implements IOperationBehavior.

Endpoint Behaviour

- Applies to the endpoints and its EndPointDispatcher.
- Implements IEndpointBehavior.

It is interesting to know that although these interfaces are called IXXXBehavior and follow a unified method naming scheme, we don't have an "IBehavior" interface. This is because the parameters passed into those methods are different (they are scope related).

All these interfaces have the following four operations, except the IServiceBehaviour, which is missing the ApplyClientBehaviour as it only applies to the service.

  • AddBindingParameters: You can use this method to support custom bindings by providing additional information about the entity (Contract, Service, Operation or Endpoint) to the runtime.
  • ApplyClientBehavior: Allows you to examine, modify and add insertion points to the client application.
  • ApplyDispatchBehavior: Allows you to examine, modify and add insertion points to the server application.
  • Validate: Use to validate the configuration of the entity and to make sure it supports the required behaviour and/or binding.

In order to add custom behaviour to a service, you need to follow these steps:

  • Create a class that implements the appropriate behaviour interface.

  • Implement the methods on the interface. Note that you do not have to provide functionality for every single method. For example, if you want to inspect all messages when they arrive at the server, all you need to do is to add the inspector objects in ApplyDisptachBehavior so the rest of the interface operations can be left empty.

    The behaviours do not do much on their own. WCF calls the methods you implement during the initialisation of the service just before it is opened. So you can use those methods to inject your customised behaviour into the runtime (i.e. by adding message inspectors, modifying the serialisers, etc).

  • Add the behaviour to the runtime. You can do this by:
    • Writing code: Add your behaviour to the Behaviors collection of the related entity before starting the host.
    • Decorating with an attribute: Some people prefer the declarative approach. Change your behaviour class (which implements IXXXBehavior) so that it inherits from Attribute. Then decorate the appropriate entity with the behaviour class. You cannot use this approach for Endpoint Behaviours.
    • Configuration: This is the most flexible option as it allows you to configure your service without changing the code. However, you cannot add Contract or Operation behaviours using the configuration, which makes sense as Contract/Operation behaviours have an impact on design so they should be defined and added at design time as opposed to run-time. In order to expose your behaviour through configuration, you need to create a class that derives from BehaviorExtensionElement. This page on MSDN provides more details.
Published Thursday, February 22, 2007 8:58 AM by Mehran Nikoo
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)