我已经尝试了很长时间来弄清楚这是我们的。当我尝试将我的类与拦截器绑定(bind)时,我在线上遇到以下异常
Kernel.Bind<MyClass>().ToSelf().Intercept().With<ILoggerAspect>();
加载 Ninject 组件 IAdviceFactory 时出错。在内核的组件容器中没有注册过这样的组件
我尝试过使用和不使用 LoadExtensions,使用模块来设置我的绑定(bind),我的最后一次尝试看起来像这样
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;
}
}
我的记录器属性看起来像这样
public class LogAttribute : InterceptAttribute
{
public override IInterceptor CreateInterceptor(IProxyRequest request)
{
return request.Context.Kernel.Get<ILoggerAspect>();
}
}
我的拦截器是这样的
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);
}
}
最佳答案
除了Remo Gloor's answer这使我倾向于为 Ninject.Extensions.Interception.DynamicProxy
添加 nuget 包,我一直得到与 OP 相同的异常,直到我手动加载 DynamicProxyModule
- FuncModule
也是手动加载的,以解决涉及工厂扩展的类似错误:
_kernel = new StandardKernel(
new NinjectSettings{LoadExtensions = true},
new FuncModule(),
new DynamicProxyModule()); // <~ this is what fixed it
关于ninject - 无法让 Ninject.Extensions.Interception 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9988204/