Unity Interception aka Microsoft Research Detours

Aug 17, 2011 at 2:32 PM

Years back I used the Microsoft Research Detours package to inject instrumentation to a dll -- without the caller or receiver having any awareness, I injected some of my code to run in place of a target function.

I would like to do that now in a .NET application. Namely I'd like to wrap any call to SqlCommand.ExecuteReader and ExecuteScalar (to start and this is only an example). This is for a large set of code with thousands of individual SqlCommand creations, not using the DI functionality (it came later in the project). I would like to have an instrumented log of all sql command calls, the parameters, and their durations. I could do this at the SQL side, but for now I would like to do this in the application context.

I was hoping that Unity Interception allowed me to do this -- to point Unity at the System.Data.SqlCommand type during application initialization and it would interface with the type resolver to inject my overrides in, regardless of where or how code created SqlCommand instances.. With that I would quietly implement the necessary instrumentation.

This is in FullTrust applications.

After reading the documentation and trying some samples, it doesn't seem to be working out this way. Did I misunderstand the benefits of Unity Interception?

Failing that, any suggestions on any other approach to achieving this goal?

(reposted here as I hadn't originally found this discussion group)

Aug 22, 2011 at 3:26 AM


I'm afraid Unity Interception doesn't work that way. Unity Interception is useful when you want to modify the behavior for individual objects but not the entire class as explained here. The approach I am thinking would be to intercept the call to the method where your SqlCommand resides then use the Peformace Call Handler for the instrumentation.


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us