Interception in .NET – Part 1: Introduction

Update: see part two here.

Interception is the capability by which developers can inject behavior dynamically into existing methods or properties, before, after or instead of their execution. A common paradigm is Aspect-Oriented Programming (AOP), which postulates that we separate non-core, like cross-cutting concerns, from core functionality, and we apply these concerns automatically to our code; this way developers need only focus on implementing the business requirements. These cross-cutting concerns normally consist of logging, exception handling, caching, access control and the likes. An example: imagine you want any exception that might be thrown by your code to be logged somewhere; in this case, you can create an aspect to be applied to your methods that wraps each in a trycatch block and does something with the caught exception.

In .NET, as in other object-oriented languages (think Java), we have basically two kinds of interception:

  • Static: the assembly code is changed after it is built, a process called IL weaving;
  • Dynamic: changes are done as the application is running.

Some examples of dynamic frameworks that allow injecting interception at compile or runtime include Unity, Ninject, Spring.NET, Castle Windsor, LinFu, Autofac, LOOM.NET, Seasar, etc (as you can see, these are all Inversion of Control containers). Static (post-compilation) ones include Fody, SheepAspect, Mono.Cecil, and PostSharp. There are use cases for both kinds, so one does not really exclude the other. Static interception will probably be faster, but then the resulting assembly will not be exactly what you expect it to be – it has happened to me: the .PDB file would not match the .DLL, but it was my fault! Smile. In dynamic interception you get more control over the process and can even modify it at runtime.

How exactly aspects are applied to the code depends on the framework being used: some rely on attributes, others XML configuration, and others on fluent interfaces. In any case, the general idea is:

“For methods X, Y and Z, before/after/instead of actually executing it, do this instead”

On the next post, I am going to talk about the alternatives that exist for dynamic interception in .NET. Stay tuned!

Published by

Ricardo Peres

Team Leader at Dixons Carphone. Microsoft MVP.

Leave a Reply

Your email address will not be published. Required fields are marked *