我正在学习使用 Ninject 和 Interceptor 模式。
我有以下拦截器。
public class MyInterceptor:IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("Pre Execute: " + invocation.Request.Method.Name);
foreach (var param in invocation.Request.Arguments)
{
Console.WriteLine("param : " + param);
}
invocation.Proceed();
Console.WriteLine("Post Execute: " + invocation.Request.Method.Name);
Console.WriteLine("Returned: " + invocation.ReturnValue);
}
}
并且有一个名为的类MyClass
,它除了 2 个简单的方法之外什么都没有,它们是虚拟的,以允许拦截器对其进行处理。(两种方法是 Echo 和 double,正如他们的名字所说的那样。)
我通过 NuGet 将 Ninject、Ninject.Extensions.Interception 和 Ninject.Extensions.Interception.DynamicProxy 添加到我的项目中。
添加了以下using
语句。
using Ninject;
using Ninject.Extensions.Interception.Infrastructure.Language;
using Ninject.Extensions.Interception;
我的主要方法,它进行引导看起来像这样。
static void Main(string[] args)
{
MyClass o = null;
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<MyClass>().ToSelf().Intercept().With(new MyInterceptor());
o = kernel.Get<MyClass>();
}
o.Echo("Hello World!"); // Error
o.Double(5);
}
我在指定的行收到以下错误。
Error loading Ninject component IProxyRequestFactory
No such component has been registered in the kernel's component container.
Suggestions:
1) If you have created a custom subclass for KernelBase, ensure that you have properly
implemented the AddComponents() method.
2) Ensure that you have not removed the component from the container via a call to RemoveAll().
3) Ensure you have not accidentally created more than one kernel..
谁能告诉我我做错了什么?