I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering, and Programming.
Fahrenheit Marketing is a top-dog Austin Web Design firm offering a complete portfolio of online services.
Windows Communication Foundation is one of the top technologies in the field of flexibility and customizability and those who have worked with this great product already know that a simple task can be done with several ways for different requirements and scales. WCF enables many doors to developers to choose from, and accomplish what they want.
Here I want to concentrate on one of these points that allows you to customize operation behaviors on service or client side with the least amount of work and a high level of access to information. This point is nothing but System.ServiceModel.Description.IOperationBehvaior interface which acts as an extensibility point that lets you implement its four methods in order to customize an operation behavior for various purposes.
IOperationBehavior is usually used in conjunction with other extensibility options in WCF that allow expansion of service, behavior, dispatcher and many other parts of the technology, and they’re the topic of some upcoming posts. IOperationBehavior lets you add your custom implementation for these extra features to application runtime through four methods:
One shared parameter of all these methods is OperationDescription which is actually an object that only provides some common information that you usually need when implementing IOperationBehavior.
Going back and forth, the other question that may come to your mind is how to apply your implementation of IOperationBehavior to your services. Generally, there are two ways to accomplish this.
You can add your custom implementation of this interface to the list of behaviors through the Behaviors property of OperationDescription. The place to do this varies and you may need to do it in service host events for service-side, and channel factory for client-side.
The easier way that is more common among developers is to implement this interface as an attribute, so you can embellish your service or client methods with the attribute as a replacement for default OperationBehavior class.
Having this theoretical background, let me wrap things up by applying these in action. Of course, the following example is a very basic one because on one hand the actually application of IOperationBehavior is tight to other extensibility options, so you can notice its value in my upcoming posts, and on the other hand, I was unable to find a good idea for this example that fits into the context of a blog post. However, here I write a custom operation behavior that only writes the name of the operation in Debug window.
First of all, I implement my custom operation behavior from IOperationBehavior interface. There is nothing to explain about this implementation except that I use an attribute in order to simplify the usage of my implementation.
using System;
using System.Diagnostics;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
namespace IOperationBehaviorSample
{
public class KeyvanOperationBehavior : Attribute, IOperationBehavior
{
#region IOperationBehavior Members
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
}
public void Validate(OperationDescription operationDescription)
{
Debug.WriteLine(string.Format("Name: {0}", operationDescription.Name));
}
#endregion
}
}
The next steps are pretty easy. I create a service contract and embellish my service method with KeyvanOperationBehavior attribute as operation behavior class. This is all I need to replace my operation behavior class.
using System.ServiceModel;
namespace IOperationBehaviorSample
{
[ServiceContract]
public interface IKeyvanService
{
[OperationContract]
[KeyvanOperationBehavior]
string GetData();
}
}
Finally I write the service, itself.
using System;
namespace IOperationBehaviorSample
{
public class KeyvanService : IKeyvanService
{
#region IKeyvanService Members
public string GetData()
{
return string.Format("Current time: {0}", DateTime.Now.ToString());
}
#endregion
}
}
In the end, I can test the service and see the expected text in my output.

I have uploaded the source code sample for this post here. Soon I’m going to write a series of blog posts about extending various aspects of Windows Communication Foundation where I’ll apply this interface frequently so you can see its usage in action in better examples.
Fahrenheit Marketing is your resource for Search Engine Optimization in Austin.
Dew Drop - February 20, 2009 | Alvin Ashcraft's Morning Dew
Feb 20, 2009 9:56 AM
#
Pingback from Dew Drop - February 20, 2009 | Alvin Ashcraft's Morning Dew
Use IOperationBehavior to Customize Operation Behavior in WCF : Keyvan Nayyeri
Feb 20, 2009 3:42 PM
#
Thank you for submitting this cool story - Trackback from DotNetShoutout
Use IDispatchMessageFormatter and IClientMessageFormatter to Customize Messages in WCF
Feb 23, 2009 1:15 PM
#
In one of my recent blog posts I talked about IOperationBehavior interface as an extensibility point to customize operation behavior in Windows Communication Foundation . There I pointed that this interface works in conjunction with various options in
Links
Feb 25, 2009 3:00 PM
#
Links
Leave a Comment