Presenter: Rob Daigneau
Code: http://www.designpatternsfor.net
I think it must either be Rob Daigneau or the topic of Windows Communication Foundation because this was yet again another superb talk. WCF was of little interest to me when I first arrived at this conference, however now I found myself extremely interested in learning more about how it might improve the architecture of my projects. It doesn't feel, like so many unfortunately do, like a technological solution in search of a problem. I can already see where I could apply WCF to bring a robust communication framework into the design of my applications.
Aspect-Oriented Programming
Aspects encapsulate the handling of a particular concern. The aspects can intercept the flow of program execution to inject their code and perform work. Custom behaviors in WCF borrow a lot of concepts from the aspect-oriented programming field of thought.
Why Custom Behaviors
There are several reasons for defining custom behaviors using interception logic:
- Validate parameters and messages
- Alter or Transform parameters and message
- Log information about messages
- Redirect operations to invoke - change the Action that is called based upon some part of the message body
- Custom Error Handling
- Custom Authorization
WCF Behaviors
Extensibility
The WCF runtime provides hooks for extensibility at the Sevice Model Layer and the Channel Layer.
Service Model Layer
Proxy and Dispatcher
Channel Layer
Client or Service Side
Behaviors are injected in the Service Model Layer. Extensions are added in the Channel Layer.
Framework Requirements
In order for custom behaviors to function, the .NET framework and WCF will need to be installed on the client because otherwise the .NET specific injection hooks on the client side will not function.
Interceptors
Interceptors are a means to extend the WCF runtime. The System.ServiceModel.Dispatcher contains a number of interfaces that, if implemented, can be used to define custom behaviors. By intercepting the ServiceHost.Open (on the service side) and ChannelFactory.Open (on the client side), the behaviors can be added to the WCF runtime. In this way, the flow of program execution will be intercepted at known join-points and the code within your custom behavior classes will be executed. Interceptors can also be injected at specific points on the dispatcher.
Potential Scenarios
- IParameterInspector
- IClientMessageFormatter
- IClientMessageInspector
- IDispatchMessageInspector
- IDispatchOperationsSelector
- IDispatchMessageFormatter
Loading the Behaviors into the WCF Runtime
In order to inject behaviors, their exist interfaces in the System.ServiceModel.Description namespace.
- IServiceBehavior
- IEndpointBehavior
- IContractBehavior
- IOperationBehavior
Scoping
Each of these is scope within the other, with the IServiceBehavior implementations having the power to inject behaviors on services, endpoints, contracts, and operations. Each tier down can inject behaviors on a smaller scope than the previous interface implementation, meaning that IOperationBehavior implementations can only inject behaviors on operations.
Means on Injection
Behaviors may be injected through configuration, attributes, or programmatic means. All behaviors can be applied programmatically. Only certain behaviors can be applied via configuration or through the use of attributes.