无法加载IProxyRequestFactory

无法加载IProxyRequestFactory

本文介绍了与Ninject进行拦截.无法加载IProxyRequestFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用Ninject和Interceptor模式.

I'm learning to use Ninject and Interceptor pattern.

我有以下拦截器.

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,它们的名称相同.)

And have a class named MyClass which got nothing but 2 simple methods, virtual to allow the interceptors to work on them. (Two methods are Echo and double, which does what their name says.)

我通过NuGet将Ninject,Ninject.Extensions.Interception和Ninject.Extensions.Interception.DynamicProxy添加到我的项目中.

I added Ninject, Ninject.Extensions.Interception and Ninject.Extensions.Interception.DynamicProxy to my project via NuGet.

添加了以下using语句.

using Ninject;
using Ninject.Extensions.Interception.Infrastructure.Language;
using Ninject.Extensions.Interception;

我的Main方法,它进行引导,如下所示.

My Main method, which does the bootstrapping looks like this.

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

在指定的行出现以下错误.

I'm getting the following error at the specified line.

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..

有人可以告诉我我在做什么错吗?

Can anyone tell me what I'm doing wrong ?

推荐答案

好,我终于能够重现(忘了将MyClass方法虚拟化).我解决问题的方法是从内核周围删除using块:

OK, I was finally able to reproduce (forgot to make the MyClass methods virtual). The way I solved it was by removing the using block from around the kernel:

    static void Main(string[] args)
    {
        MyClass o = null;

        var kernel = new StandardKernel();
        kernel.Bind<MyClass>().ToSelf().Intercept().With(new MyInterceptor());
        o = kernel.Get<MyClass>();

        o.Echo("Hello World!"); // Error
        o.Double(5);
        Console.ReadKey(true);
    }

我之所以进行猜测的原因是,它在幕后为MyClass创建了一个代理类,并以某种方式将IKernel传递给了代理.当您调用方法(在代理服务器上)时,它会返回内核并解析一些其他依赖项(包括IProxyRequestFactory).由于您正在处理它,因此它不再能够解决该依赖性.

The reason I'm guessing this works is because under the covers it creates a proxy class for MyClass and somehow passes in the IKernel to the proxy. When you invoke the method (on the proxy) it goes back to the kernel and resolves some additional dependencies (including the IProxyRequestFactory). Since you were disposing of it, it was no longer able to resolve that dependency.

这篇关于与Ninject进行拦截.无法加载IProxyRequestFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:21