Can't get Ninject.Extensions.Interception working

淺唱寂寞╮ 提交于 2020-08-02 05:53:48

问题


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);
    }
}

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/9988204/cant-get-ninject-extensions-interception-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!