Igor Kulman Igor Kulman -4 years ago 149
ASP.NET (C#) Question

Getting info about methods intercepted by Ninject

I am using Ninject interception to log errors on some of my methods. My interception class looks like this

public class ErrorLoggingInterceptor : IInterceptor
private readonly ILogFactory _logFactory;

public ErrorLoggingInterceptor(ILogFactory logFactory)
_logFactory = logFactory;

public void Intercept(IInvocation invocation)
catch (Exception e)
var sb = new StringBuilder();
sb.AppendFormat("Executing {0}.{1} ",invocation.Request.Method.DeclaringType.Name,invocation.Request.Method.Name);
sb.AppendFormat(" {0} caught: {1})", e.GetType().Name, e.Message);

This interceptor class works just fine but there are a few problems I came across.

  1. invocation.Request.Method.DeclaringType.Name
    gives me the name of the interface, how to get the name of the real impementing class?

  2. is there a way to get the argument values? I can get the parameter names using
    but I did not found a way to get the actual values

  3. my interceptor is intended to "swallow" exceptions. It works on void methods, but is there a way to make it work on non-void methods providing a default value as the result? Say I have a
    bool DoSomething(..)
    and when it fails with exception I want it to look like the method returned false.

Answer Source

You're talking about Ninject but I assume that you're only interested in features of Castle Dynamic Proxy and by IInvocation you mean Castle.DynamicProxy.IInvocation.

  1. invocation.TargetType.Name
  2. invocation.Arguments
  3. invocation.ReturnValue - you may set it when exception occurs


When it comes to Ninject extensions I would expect something similar (however, I've never used it):

  1. invocation.Request.Target.GetType().Name
  2. invocation.Request.Arguments
  3. invocation.ReturnValue

https://github.com/ninject/ninject.extensions.interception/blob/master/src/Ninject.Extensions.Interception/IInvocation.cs https://github.com/ninject/ninject.extensions.interception/blob/master/src/Ninject.Extensions.Interception/Request/IProxyRequest.cs

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download