简体   繁体   English

具有类型参数的Autofac DynamicProxy

[英]Autofac DynamicProxy with type parameters

I want to use Autofac's type interception to cache method results. 我想使用Autofac的类型拦截来缓存方法结果。

I registered my types with code below 我在下面的代码中注册了我的类型

builder.RegisterAssemblyTypes(dependentAssemblies)
            .Where(x => x.GetCustomAttributes(typeof(InterceptAttribute), true).Any())
            .WithParameters(parameters)
            .AsImplementedInterfaces()
            .EnableInterfaceInterceptors()
            .InstancePerRequest();

And it works fine. 而且效果很好。 But this is Interface interception. 但这是接口拦截。 When i register types with 当我注册类型

builder.RegisterAssemblyTypes(dependentAssemblies)
            .Where(x => x.GetCustomAttributes(typeof(InterceptAttribute), true).Any())
            .WithParameters(parameters)
            .AsImplementedInterfaces()
            .EnableClassInterceptors()
            .InstancePerRequest();

I get error 我得到错误

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Castle.Proxies.MyServiceProxy' can be invoked with the available services and parameters: Cannot resolve parameter 'System.String ' of constructor 'Void .ctor(Castle.DynamicProxy.IInterceptor[], System.String, System.String, NLog.ILogger)'. 无法使用可用服务和参数调用类型为“ Castle.Proxies.MyServiceProxy”的与“ Autofac.Core.Activators.Reflection.DefaultConstructorFinder”匹配的构造函数:无法解析构造函数“ Void .ctor”的参数“ System.String” (Castle.DynamicProxy.IInterceptor [],System.String,System.String,NLog.ILogger))。

My params list is equal in both cases and is enough to create MyService and looks like 我的参数列表在两种情况下都是相等的,足以创建MyService,看起来像

var parameters = new[]
        {
            new NamedParameter("name1", "value1"),
            new NamedParameter("name2", "value2")
        };

Have i forgot something? 我忘了什么吗?

The interceptors get wrapped around services (the As<T> part), not components (the concrete thing getting registered). 拦截器围绕服务As<T>部分)而不是组件 (注册具体事物)。 You're registering things as interfaces but you're not enabling interface interception , you're enabling class interception . 您正在将事物注册为接口,但未启用接口拦截 ,而是启用了类拦截 There aren't any classes to intercept. 没有任何要拦截的类。

Either also add .AsSelf() to that registration statement or switch back to EnableInterfaceInterceptors() . 可以在该注册语句中添加.AsSelf()或切换回EnableInterfaceInterceptors()

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

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