C# 使用autofac和DynamicProxy 2的选择性拦截方法

C# 使用autofac和DynamicProxy 2的选择性拦截方法,c#,autofac,castle-dynamicproxy,C#,Autofac,Castle Dynamicproxy,我目前正在使用Autofac-1.4.5.676、Autofac contrib和castle DynamicProxy 2进行一些实验。目标是创建一个粗粒度分析器,它可以拦截对特定接口的特定方法的调用 问题是:除了选择性部分之外,我所有的东西都工作得很好。我可能错了,但我认为我需要将拦截器与IProxyGenerationHook实现结合起来,但我不知道如何做到这一点 我的代码如下所示: 要截取和分析的接口(注意,我只关心分析Update()方法) 现在,当我向容器注册系统时,我会执行以下操作

我目前正在使用Autofac-1.4.5.676、Autofac contrib和castle DynamicProxy 2进行一些实验。目标是创建一个粗粒度分析器,它可以拦截对特定接口的特定方法的调用

问题是:除了选择性部分之外,我所有的东西都工作得很好。我可能错了,但我认为我需要将拦截器与IProxyGenerationHook实现结合起来,但我不知道如何做到这一点

我的代码如下所示:

要截取和分析的接口(注意,我只关心分析Update()方法)

现在,当我向容器注册系统时,我会执行以下操作:

// Register interceptor gubbins
builder.RegisterModule(new FlexibleInterceptionModule());
builder.Register<PerformanceInterceptor>();

// Register systems (just one in this example)
builder.Register<AudioSystem>()
.As<ISomeSystemToMonitor>)
.InterceptedBy(typeof(PerformanceInterceptor)); 
//注册拦截器gubbins
RegisterModule(新的FlexibleInterceptionModule());
builder.Register();
//注册系统(本例中仅一个)
builder.Register()
.As)
.被(性能接收器)截取的类型;
从容器中拉出的所有ISomeSystemToMonitor实例都会根据需要进行拦截和分析,而不仅仅是拦截其所有方法,而不仅仅是Update方法

现在,我如何扩展它以排除Update()以外的所有方法?正如我所说,我不明白我是如何通知容器“对于ProfileInterceptor,使用IProxyHookGenerator的这个实现”


感谢大家的帮助,干杯!另外,请注意,我现在无法升级到autofac2.x;我被1卡住了。

在生成拦截器时,必须将
IProxyGenerationHook
实例传递给
CreateInterfaceProxyWithTarget
调用。有关更多详细信息,请参阅

目前,似乎没有一种方法可以在不更改Autofac.DynamicProxy 2集成模块的情况下提供此类挂钩。可能是对扩展拦截的
的一个很好的添加


或者,您可以将筛选构建到
PerformanceInterceptor
中。查看调用时传递的
IInvocation
,检查
方法
属性以决定是否配置文件。但是这当然比绕过代理级别的拦截效率要低。

对于DynamicProxy 2,启用InterfaceInterceptors
方法现在有一个重载,它接受一个
ProxyGenerationOptions
对象

//Define the builder
var builder = new ContainerBuilder();

//Instantiate your Proxy options with a selector
var proxyOptions = new ProxyGenerationOptions {Selector = new MyInterceptSelector()};

//Pass the proxy options as a parameter to the EnableInterfaceInterceptors method
builder.RegisterType<MyRepo>()
            .As<IMyRepo>()
            .EnableInterfaceInterceptors(proxyOptions)
            .InterceptedBy(typeof(IInterceptor));
//定义生成器
var builder=new ContainerBuilder();
//使用选择器实例化代理选项
var proxyOptions=new ProxyGenerationOptions{Selector=new MyInterceptSelector()};
//将代理选项作为参数传递给EnableInterfaceInterceptors方法
builder.RegisterType()
.As()
.EnableInterfaceInterceptors(代理选项)
.被(interceptor的类型)截取;

彼得,谢谢你的回答。由于这是为了代码分析,我希望它尽可能轻,所以我想我必须检查在拦截器内执行检查的性能,并检查其灵活性等。如果您觉得可以,您可以获取AutofacContrib.dynamicproxy 2源代码并手动添加挂钩。这样,您至少可以在拦截器中使用钩子与过滤进行一些比较。我知道这是一个老问题,但对于正在阅读此问题的人来说,使用较新版本的DynamicProxy2,现在可以使用
.EnableClassInterceptors(new ProxyGenerationOptions(hook))
来指定用于HeadUp的钩子,@CyrilDurand
//Define the builder
var builder = new ContainerBuilder();

//Instantiate your Proxy options with a selector
var proxyOptions = new ProxyGenerationOptions {Selector = new MyInterceptSelector()};

//Pass the proxy options as a parameter to the EnableInterfaceInterceptors method
builder.RegisterType<MyRepo>()
            .As<IMyRepo>()
            .EnableInterfaceInterceptors(proxyOptions)
            .InterceptedBy(typeof(IInterceptor));