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, ...

Get Programming WCF Services, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.