Skip to Content
Programming WCF Services, 3rd Edition
book

Programming WCF Services, 3rd Edition

by Juval Lowy
August 2010
Intermediate to advanced
908 pages
26h 22m
English
O'Reilly Media, Inc.
Content preview from Programming WCF Services, 3rd Edition

Intercepting Client Calls

To intercept client-side calls, WCF provides the interface IClientMessageInspector, defined as:

public interface IClientMessageInspector
{
   object BeforeSendRequest(ref Message request,IClientChannel channel);
   void AfterReceiveReply(ref Message reply,object correlationState);
}

The BeforeSendRequest() method is called just before the message is sent down the wire, allowing you to affect the request message. Similarly, the AfterReceiveReply() method is your chance to interact with the reply message for post-call processing.

The client runtime represented by the ClientRuntime class contains a collection of message inspectors:

public sealed class ClientRuntime
{
   public SynchronizedCollection<IClientMessageInspector> MessageInspectors
   {get;}

  //More members
}

You can add your message inspector to the collection by associating the proxy with an endpoint behavior. That behavior needs to add the inspector in the ApplyClientBehavior() method:

public interface IEndpointBehavior
{
   void ApplyClientBehavior(ServiceEndpoint endpoint,ClientRuntime clientRuntime);
   //More members
}

To encapsulate these steps I wrote the class InterceptorClientBase<T>, defined in Example E-4.

Example E-4. The InterceptorClientBase<T> class

public abstract class InterceptorClientBase<T> : ClientBase<T> where T : class { public InterceptorClientBase() { Endpoint.Behaviors.Add(new ClientInterceptor(this)); } public InterceptorClientBase(string endpointName) : base(endpointName) { Endpoint.Behaviors.Add(new ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming WCF Services, 4th Edition

Programming WCF Services, 4th Edition

Juval Lowy, Michael Montgomery
Programming .NET Security

Programming .NET Security

Adam Freeman, Allen Jones

Publisher Resources

ISBN: 9781449382476Supplemental ContentErrata Page