c# - Autofac RegisterType 不提供 EnableInterfaceInterceptors

标签 c# aop autofac interceptor

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

我从以下基本行为开始,想在 Autofac 中注册一个测试类及其接口(interface):

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()请问不工作吗?

最佳答案

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

关于c# - Autofac RegisterType 不提供 EnableInterfaceInterceptors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40610000/

相关文章:

c# - 如何将参数添加到方法并覆盖子类

c# - 创建挂接到属性的 getter 和 setter 的 C# 属性

SpringAOP 生成的动态子类缺少注解

class - 如何在 AOP(面向方面​​的编程)中使用单例类?

c# - 具有大量 HDI 参数的多态抽象类

c# - 使用 Autofac 拦截 .NET Core 2.0 Web API Controller

c# - 这个 2 字节到 int 的转换有什么问题?

c# - 自定义自动生成的部分类

dependency-injection - 具有开放泛型和运行时指定类型的 Autofac

c# - 实现接口(interface) B 的接口(interface) D 的列表不能被识别为 List<B>