Can't get Ninject.Extensions.Interception working
Asked Answered
D

2

21

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);
    }
}
Discovert answered 3/4, 2012 at 6:31 Comment(0)
M
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.

Moll answered 3/4, 2012 at 7:2 Comment(5)
I tried grabbing them with Nuget, do you need both or just one?Discovert
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.Discovert
Useful to know, I was trying to get it to work using LoadExtensions=false and couldn't get an instance of anything from the kernelAnzio
Do you need the Kernel.Bind<MyClass>().ToSelf().Intercept().With<ILoggerAspect>(); 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?Beckford
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.Beckford
R
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
Rosalindrosalinda answered 27/6, 2016 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.