我正在学习使用Ninject和拦截模式。
我有以下的拦截器。
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种简单的方法,虚拟允许拦截对他们的工作。 (两种方法是回声,双,这做什么他们的名字说。)
我加了Ninject,Ninject.Extensions.Interception和Ninject.Extensions.Interception.DynamicProxy通过的NuGet我的项目。
添加以下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..
谁能告诉我,我做错了什么?