20

I've been trying for ages to figure this our. when i try to bind my class with an interceptor i'm getting the following exception on the line

Kernel.Bind<MyClass>().ToSelf().Intercept().With<ILoggerAspect>();

Error loading Ninject component IAdviceFactory. No such component has been registered in the kernel's component container

I've tried with and without LoadExtensions, With about with using a Module to set up my bindings and my last attempt looks like this

internal class AppConfiguration 
{

    internal AppConfiguration( )
    {
        var settings = new NinjectSettings() { LoadExtensions = false };
        Kernel = new StandardKernel(settings);
        Load();
    }

    internal StandardKernel Kernel { get; set; }

    public static AppConfiguration Instance
    {
        get { return _instance ?? (_instance = new AppConfiguration()); }
    }

    private static AppConfiguration _instance;

    private void Load()
    {
        Kernel.Bind<ILoggerAspect>().To<Log4NetAspect>().InSingletonScope();
        Kernel.Bind<MyClass>().ToSelf().Intercept().With<ILoggerAspect>();
    }

    internal static StandardKernel Resolver()
    {
        return Instance.Kernel;
    }
}

My Logger Attribute looks like this

public class LogAttribute : InterceptAttribute
{
    public override IInterceptor CreateInterceptor(IProxyRequest request)
    {
        return request.Context.Kernel.Get<ILoggerAspect>();
    }
}

And my interceptor like this

 public class Log4NetAspect : SimpleInterceptor, ILoggerAspect
{
    protected override void BeforeInvoke(IInvocation invocation)
    {
        Debug.WriteLine("Running " + invocation.ReturnValue);
        base.BeforeInvoke(invocation);
    }

    public new void Intercept(IInvocation invocation)
    {
        try
        {
            base.Intercept(invocation);
        }
        catch (Exception e)
        {
            Debug.WriteLine("Exception: " + e.Message);
        }
    }

    protected override void AfterInvoke(IInvocation invocation)
    {
        Debug.WriteLine("After Method");
        base.AfterInvoke(invocation);
    }
}
Ryan Burnham
  • 2,619
  • 3
  • 27
  • 43

2 Answers2

30

Most likely you didn't deploy Ninject.Extensions.Interception.DynamicProxy or Ninject.Extensions.Interception.Linfu alongside your application [and Ninject.Extensions.Interception]. You have to pick exactly one of them.

With the code as you have it right now (LoadExtensions=false) it will fail to pick up the specific interception library - you should remove that and the normal extensions loading should wire the extension into the Kernel on creation for the interception bits to pick it up.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • I tried grabbing them with Nuget, do you need both or just one? – Ryan Burnham Apr 03 '12 at 07:53
  • 2
    Thanks that was it. Why doesn't the nuget package just add one of these as a Default? from the developer using it it makes no difference what it uses. – Ryan Burnham Apr 04 '12 at 00:05
  • Useful to know, I was trying to get it to work using LoadExtensions=false and couldn't get an instance of anything from the kernel – user1039513 Aug 29 '12 at 08:06
  • Do you need the Kernel.Bind().ToSelf().Intercept().With(); as well as the LoggerAttribute ?? I can't get this working either, but I was under the impression that you only need the attribute OR the bind and not both? – Robert Noack Nov 21 '14 at 17:06
  • To add to my comment above I can confirm you don't need both. If you add the Kernel.Bind *and* the attribute, your interceptor will fire twice. – Robert Noack Nov 21 '14 at 18:19
3

In addition to Remo Gloor's answer which pointed me toward adding the nuget package for Ninject.Extensions.Interception.DynamicProxy, I kept getting the same exception as the OP, until I manually loaded a DynamicProxyModule - the FuncModule is manually loaded as well, to work around a similar error involving the factory extension:

_kernel = new StandardKernel(
    new NinjectSettings{LoadExtensions = true}, 
    new FuncModule(), 
    new DynamicProxyModule()); // <~ this is what fixed it
Community
  • 1
  • 1
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235