2

I have Ninject, Ninject.Extensions.Interception and Ninject.Extensions.Interception.DynamicProxy installed through NuGet and I have the following module

public class InterceptAllModule : InterceptionModule
{
    public override void Load()
    {
        Kernel.Intercept(p => (true)).With(new TimingInterceptor());
    }
}

Where TimingInterceptor is

public class TimingInterceptor : SimpleInterceptor
{
    readonly Stopwatch _stopwatch = new Stopwatch();
    protected override void BeforeInvoke(IInvocation invocation)
    {
        _stopwatch.Start();
    }

    protected override void AfterInvoke(IInvocation invocation)
    {
        _stopwatch.Stop();
        string message = string.Format("[Execution of {0} took {1}.]",invocation.Request.Method,_stopwatch.Elapsed);
        Log.Info(message + "\n");
        _stopwatch.Reset();
    }
}

Now, when I try to hook the module up with ninject kernel, and run my site

var kernel = new StandardKernel(new InterceptAllModule());

I got the following error,

Could not load type 'Castle.Proxies.Func`1Proxy' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a621a9e7e5c32e69' because the parent type is sealed.

Basically I'm trying to do logging for my ASP.NET MVC app whenever an action method is invoke I want to log certain things. But don't know how to get around this error. Could someone with experience point out what I'm doing wrong please? Thanks.

Ray
  • 12,101
  • 27
  • 95
  • 137
  • 2
    I bet `p => (true)` means 'intercept everything', and now Ninject is trying to intercept a sealed type. Are you registering a concrete (sealed) type `AsSelf()`? – Steven Dec 27 '12 at 19:46
  • 1
    Thanks Steven you are correct. I should re-word my question to how do I setup intercept with ninject on all the controller action method calls? Do you happen to have lines of code that could show me in that regard? Thanks a lot! – Ray Dec 27 '12 at 22:04
  • I'm sorry. Can't help with that. Try using IntelliSense on that `p` argument. – Steven Dec 27 '12 at 23:11
  • You can find a quite detailed implementation example here: http://stackoverflow.com/questions/14062028/how-to-intercept-all-the-asp-net-webapi-controller-action-methods-calls-with-nin – Sebastian Meier Mar 20 '15 at 22:59

0 Answers0