O'Reilly logo

Programming WCF Services, 3rd Edition by Juval Lowy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Intercepting Service Operations

Recall from Chapter 1 that in the abstract, all WCF does when intercepting calls is perform pre- and post-call operations. Adding custom steps to this interception mechanism is probably the most common way of extending WCF.

Every endpoint dispatcher has a reference to an interface called IOperationInvoker, defined as:

public interface IOperationInvoker
{
   object[] AllocateInputs();
   object Invoke(object instance,object[] inputs,out object[] outputs);

   //Asynchronous invocation methods
}

The dispatcher uses the Invoke() method to invoke the calls on the service instance. In providing for the invocation, IOperationInvoker is the right place to plug in your code. Specifically, assigning the dispatcher your implementation of IOperationInvoker will enable you to hook it in.

The Generic Invoker

The first step in implementing my generic interceptor framework was to provide an abstract implementation of IOperationInvoker that enables custom pre- and post-call steps, as shown in Example E-1.

Example E-1. The GenericInvoker class

public abstract class GenericInvoker : IOperationInvoker { readonly IOperationInvoker m_OldInvoker; public GenericInvoker(IOperationInvoker oldInvoker) { m_OldInvoker = oldInvoker; } public virtual object[] AllocateInputs() { return m_OldInvoker.AllocateInputs(); } protected virtual void PreInvoke(object instance,object[] inputs) {} //Always called, even if operation had an exception protected virtual void PostInvoke(object instance,object returnedValue, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required