Thank you for recommending "Dev Pro provides comprehensive, independent how-to content on building applications with the Microsoft stack.".
Your recommendation has been successfully processed.
July 27, 2010 09:04 AM
Creating Custom WCF Behaviors
How to create custom service and endpoint behaviors for your WCF services
Dev Pro
InstantDoc ID #125546
Services implement the logic for the operations defined by their service contract. Service endpoints describe which contract is in effect, what protocols should be employed, and the address used to reach the service. Any additional logic or configuration defining how the service should process messages that are applied locally to the service is encapsulated in a service behavior. Common examples of service behaviors you may have seen include the serviceMetadata behavior which is used to expose a WSDL for your services in support of proxy creation (such as that used by Visual Studio’s Add Service Reference), or serviceDebug which allows you to enable the inclusion of exception details in faults. Another common scenario for applying a custom service behavior is when your service is being hosted within AppFabric, and you need to apply additional configuration to it in the small window of time between when WAS initializes the service instance and when the service host is opened.
On the client side, the story is similar. One can use endpoint behaviors to alter the way the client proxy processes messages. An endpoint behavior is an easy way to plug in a Client Message Inspector to provide additional processing around when a message is sent or received by the client-side proxy.
In the article, we'll walk through the creation of both a custom service behavior and endpoint behavior, how to apply them via their respective configuration via *.config files, and what additional steps you must take when using them with AppFabric.
Creating a Custom Service Behavior
To illustrate the steps you need to follow to create a custom service behavior, we will pretend we are faced with a situation where we need to add an extension to the ServiceHost after it is initialized but before it opens. For example, in the documentation on using Promotable Properties of WCF Workflow Services, there is a sample that shows how you create an instance of a SqlWorkflowInstanceStoreBehavior, configure it, and call a method on it all before the host is opened. If you are self hosting the code is easy, as Figure 1 shows.