MEFing WCF Extensibility
You can download the code here
Wouldn’t it be nice to extend WCF just by inheriting the relevant interface (IServiceBehavior, IDispatchMessageInspector, ext…) and having it hooked without wasting your time on finding the right way of hooking it?
Well this post is all about this topic.
How does it work?
I wrote 2 helper project MEFContracts and WcfPrimitivePlugins which responsible for that magic, and you will also need reference to MEF.
For any of your WCF services or client proxy, you have to add reference to the MEFContracts project and ComponentModel project (which is the MEF CTP 6 implementation), then place the WcfPrimitivePlugins.dll under relative folder called “Plug-Ins” (you can change this place by adding MEF catalog using ).
Modify your client proxy to inherit from MefWcfProxy<TChannel> instead of ClientBase<TChannel>.
On the service side you have 2 option:
1) using MefHost for hosting your service.
2) decorate your service with [MefBehavior] attribute
Writing your WCF extensibility
Writing WCF extensibility is made simple.
Just create class library application and implement the WCF extensibility interface
optional: add reference to MEFContracts for having advance hooking for timeout, Throttling, and more
Running the sample
The solution can be compile with different compiler switches for demonstrating different scenarios:
- ByAttribute (will hook the service using [MefBehavior] attribute)
- Debug (will hook the service using MefHost)
- IgnoreInterception (will detach the hooking)
- LimitedThrottling (will cause exceptions by limiting the concurrent session and reduce the timeout – that how you see the IErrorHandler plug-in in action)
So choose the compiler switch (recommended: delete the bin folder), compile the solution and run (WcfClientConsole and WcfHostConsole projects are configure the start with F5)
Writing WCF extension should be focus on extension and the hooking should be transparent.