简体   繁体   English

Ninject拦截动态代理问题

[英]Ninject Interception dynamic proxy problems

I'm trying to set up interception to work with Ninject which we have been using as our dependency injection framework for a while. 我正在尝试建立与Ninject配合使用的拦截功能,我们已经将Ninject用作依赖注入框架已有一段时间了。

I have downloaded the interception extension from NuGet and tried it with both the Castle Dynamicproxy implementation and the LinFu implementation but could not be either to work with our applications. 我已经从NuGet下载了拦截扩展,并在Castle Dynamicproxy实现和LinFu实现中进行了尝试,但是都无法与我们的应用程序一起使用。

Castle gave an error when creating a proxy on a class that did not have a parameterless constructor, since all the service objects have their dependencies injected via the constructor this is a problem. 在没有无参数构造函数的类上创建代理时,Castle给出了一个错误,因为所有服务对象都通过构造函数注入了其依赖项,所以这是一个问题。 The error is: 错误是:

System.ArgumentException: Can not instantiate proxy of class: emedia.RapidSystems.Subscriber.Presenters.RRSubmissionPresenter. System.ArgumentException:无法实例化类的代理:emedia.RapidSystems.Subscriber.Presenters.RRSubmissionPresenter。 Could not find a parameterless constructor. 找不到无参数的构造函数。 Parameter name: constructorArguments 参数名称:constructorArguments

The LinFu interceptor worked better, right up until the code called a method with a generic parameter then it gave me the following: LinFu拦截器工作得更好,直到代码调用了带有通用参数的方法,然后它给了我以下信息:

System.ArgumentException: Generic types are not valid. System.ArgumentException:通用类型无效。 Parameter name: methodInfo 参数名称:methodInfo

Here is a simplified version code for one of the classes I am trying to intercept: 这是我尝试拦截的类之一的简化​​版本代码:

[LogCalls]
public class Repository<T> : IRepository<T>
        where T : class
{   
    public virtual T GetEntity<TKey>(ObjectContext context, TKey key)
    {
        var entity = GetEntity(context, key, _emptyLoadingStrategy);
        return entity;
    }

    public virtual IQueryable<T> GetAll(ObjectContext context)
    {
        var query = GetAll(context, _emptyLoadingStrategy);
        return query;
    }

    public virtual T Add(ObjectContext context, T entity)
    {
        context.AddObject(EntitySetName(context), entity);
        return entity;
    }

     //other code goes here

}

Add and GetAll work fine but the error happens when GetEntity is called on the proxy. Add和GetAll可以正常工作,但是在代理上调用GetEntity时会发生错误。

At this point I'm stuck, as neither interceptor works with the code base. 在这一点上,我很困,因为两种拦截器都无法使用代码库。 Has anyone got Ninject interception working with a real complex production system, rather than a simple demo class, and if so how? 有没有人让Ninject拦截与真正的复杂生产系统一起工作,而不是与简单的演示类一起工作,如果是的话,怎么办? I don't mind which interceptor I use as long as it works. 我不介意我使用哪种拦截器,只要它能正常工作。

Or is interception with Ninject just not mature enough yet, and do I need to look at replacing the whole thing with something else like Unity? 还是用Ninject进行拦截还不够成熟,我是否需要考虑用Unity之类的东西替换整个东西?

Use version 3.0.0-rc2. 使用版本3.0.0-rc2。 it adds support for interface proxies to dynamic proxy 它为动态代理增加了对接口代理的支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM