2016-11-15 269 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版本,所以我忽略了這一點。謝謝特拉維斯。 –