【问题标题】:Problems with Castle dynamic proxy + Ninject as DICastle 动态代理 + Ninject 作为 DI 的问题
【发布时间】:2013-03-06 12:50:34
【问题描述】:

我试图为我的项目构建良好的架构,我决定使用 Ninject 作为 DI 并使用 Castle project dinamic proxy 为我的存储库添加缓存。不幸的是,我得到了一个例外。这是我的代码:

public class NinjectImplementation : NinjectModule
{
    public override void Load()
    {
        // Binding repositories
        var assembly = Assembly.GetAssembly(typeof(UserRepository));
        var types = assembly.GetTypes()
             .Where(t => t.Name.EndsWith("Repository") && !t.Name.StartsWith("I"));
        ProxyGenerator generator = new ProxyGenerator();
        //CacheInterceptor cacheInterceptor = 
        foreach (var type in types)
        {
            var interfaceType = type.GetInterfaces().Single();
            var typeWithCaching = generator.CreateClassProxy(type, new MyTestShop.Infrastructure.Caching.CacheInterceptor());

            Bind(interfaceType).To(typeWithCaching.GetType()).InThreadScope();
        }
        ...//Service layer injection
    }
}

所以我注入的不是我的存储库的实现,而是存储库的代理类(带有缓存)。

这是我的IInterceptor 实现Castle dinamic proxy

[Serializable]
public class CacheInterceptor : IInterceptor
{

    public void Intercept(IInvocation invocation)
    {
        int argumentCount = invocation.Arguments.Length;
        if (argumentCount > 1)
        {
            invocation.Proceed();
            return;
        }
        String methodNameInLower = invocation.Method.Name.ToLower();
        if (methodNameInLower.StartsWith("get"))
        {
            String cachePath = invocation.TargetType.FullName + "_" + invocation.Method.Name + "_" + invocation.Arguments[0].ToString();
            CacheHelper.Get(cachePath);
            //DO SOMETHING
            return;
        }

    }
}

我在Ninject DI container_kernel.Get<T>() 方法中得到的异常:

  • 使用条件隐式自绑定激活 IInterceptor 时出错 的 IInterceptor Provider 返回 null。*

激活路径: 3) 将依赖 IInterceptor 注入到 UserRepositoryProxy 类型的构造函数的参数中 2)将依赖IUserRepository注入到UserService类型的构造函数的参数userRepository中 1) 请求 IUserService

建议: 1) 确保提供者正确处理创建请求。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:Ninject.ActivationException:使用 IInterceptor 的条件隐式自绑定激活 IInterceptor 时出错 提供者返回 null。 激活路径: 3) 将依赖 IInterceptor 注入到 UserRepositoryProxy 类型的构造函数的参数中 2)将依赖IUserRepository注入到UserService类型的构造函数的参数userRepository中 1) 请求 IUserService

建议: 1) 确保提供者正确处理创建请求。

【问题讨论】:

  • 有内部异常吗?也许有堆栈跟踪?

标签: c# .net dependency-injection ninject castle-dynamicproxy


【解决方案1】:

我终于找到了我的问题的答案。问题是我的代理不是类型而是类型的实例,所以我这样修复它:

var interfaceType = type.GetInterfaces().Single();

var proxy = generator.CreateClassProxy(type,
    new Type[] { interfaceType },
    new IInterceptor[]
    {
        new CacheInterceptor(), 
        new LoggingInterceptor()
    });

// I'm using directive ToConstant(..), and not To(..)
Bind(interfaceType).ToConstant(proxy).InThreadScope();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 2011-01-20
    • 1970-01-01
    • 2013-02-11
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    相关资源
    最近更新 更多