2016-11-15 66 views
0

我想使用Autofac来探索自定义拦截器。我目前使用Autofac的4.2.0版和Castle.Core的Dynamic 3.3版的3.3.3版。Autofac无法使用EnableInterfaceInterceptors RegisterType

我已经开始了希望凭借其在Autofac接口注册一个测试类的以下基本动作:

using Autofac; 
using Castle.DynamicProxy; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     ContainerBuilder builder = new ContainerBuilder(); 
     builder.RegisterType<MyClassA>() 
      .As<IMyInterface>() 
      .EnableInterfaceInterceptors() 
      .InterceptedBy(typeof(MyInterceptor)); 
     builder.RegisterType<MyInterceptor>().AsSelf(); 
     var container = builder.Build(); 
    } 
} 

的问题是,“.EnableInterfaceInterceptors()”行了红色错误波浪线在其下方,错误如下:

​​

的代码的其它组分迄今(在情况下,它相关的)是:

public interface IMyInterface 
{ 
    void DoWork(string key1, string key2); 
} 


using System; 

public class MyClassA : IMyInterface 
{ 
    public void DoWork(string key1, string key2) 
    { 
     Console.WriteLine(string.Format("A: {0} - {1}", key1, key2)); 
    } 
} 


using System; 
using Castle.DynamicProxy; 

public class MyInterceptor : StandardInterceptor 
{ 
    protected override void PreProceed(IInvocation invocation) 
    { 
     Console.Write("PreProceed: "); 
    } 
} 

有人可以告诉我为什么.EnableInterfaceInterceptors()不能正常工作吗?

回答

2

我猜你忘了引用Autofac.Extras.DynamicProxy包。 See the docs here.

+0

我错误地认为DynamicProxy2是其后继者,并且不支持更新的Autofac版本,所以我忽略了这一点。谢谢特拉维斯。 –