Autofac RegisterType中不提供EnableInterfaceInterceptors

时间:2016-11-15 12:36:26

标签: c# aop autofac interceptor

我想使用Autofac探索自定义拦截器。我目前正在使用版本4.2.0的Autofac和Castle.Core版本3.3.3 for DynamicProxy。

我开始时想要在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()”行下面有一条红色错误波浪线,并出现以下错误:

'IRegistrationBuilder<MyClassA, ConcreteReflectionActivatorData, SingleRegistrationStyle>' does not contain a definition for 'EnableInterfaceInterceptors' and no extension method 'EnableInterfaceInterceptors accepting a first argument of type 'IRegistrationBuilder<MyClassA, ConcreteReflectionActivatorData, SingleRegistrationStyleA>' could be found (are you missing a using directive or an assembly reference?)

到目前为止,其他组件的代码(如果相关)是:

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()无效吗?

1 个答案:

答案 0 :(得分:4)

我猜你忘了引用Predicates.not包了。 See the docs here.