简体   繁体   English

使用autofac和dynamicproxy2的选择性拦截方法

[英]Selectively intercepting methods using autofac and dynamicproxy2

I'm currently doing a bit of experimenting using Autofac-1.4.5.676, autofac contrib and castle DynamicProxy2. 我目前正在使用Autofac-1.4.5.676,autofac contrib和Castle DynamicProxy2做一些实验。 The goal is to create a coarse-grained profiler that can intercept calls to specific methods of a particular interface. 目的是创建一个粗粒度的探查器,该探查器可以拦截对特定接口的特定方法的调用。

The problem: I have everything working perfectly apart from the selective part. 问题:除选择性部分外,我所有工作均正常进行。 I could be wrong, but I think I need to marry up my interceptor with an IProxyGenerationHook implementation, but I can't figure out how to do this. 我可能是错的,但是我认为我需要将拦截器与IProxyGenerationHook实现结合起来,但是我不知道该怎么做。

My code looks something like this: 我的代码如下所示:

The interface that is to be intercepted & profiled (note that I only care about profiling the Update() method) 将被拦截和分析的接口(请注意,我只关心对Update()方法进行概要分析)

public interface ISomeSystemToMonitor
{
    void Update(); // this is the one I want to profile
    void SomeOtherMethodWeDontCareAboutProfiling();
}

Now, when I register my systems with the container, I do the following: 现在,当我向容器注册系统时,请执行以下操作:

// 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)); 

All ISomeSystemToMonitor instances pulled out of the container are intercepted and profiled as desired, other than the fact that it will intercept all of its methods, not just the Update method. 从容器中拉出的所有ISomeSystemToMonitor实例都将根据需要进行拦截和分析,除了事实是它将拦截所有方法,而不仅仅是Update方法。

Now, how can I extend this to exclude all methods other than Update()? 现在,如何将其扩展为排除Update()以外的所有方法? As I said, I don't understand how I'm meant to inform the container that, "for the ProfileInterceptor, use this implementation of IProxyHookGenerator". 就像我说的,我不明白我该如何告知容器“对于ProfileInterceptor,请使用IProxyHookGenerator的此实现”。

All help appreciated, cheers! 感谢所有帮助,欢呼! Also, please note that I can't upgrade to autofac2.x right now; 另外,请注意,我现在无法升级到autofac2.x。 I'm stuck with 1. 我坚持使用1。

A IProxyGenerationHook instance have to be passed to the CreateInterfaceProxyWithTarget call when the interceptor is produced. 生成拦截器时,必须将IProxyGenerationHook实例传递给CreateInterfaceProxyWithTarget调用。 See this tutorial for some more details. 有关更多详细信息,请参见本教程

Currently there doesn't seem to be a way of providing such hooks without changes to the Autofac.DynamicProxy2 integration module. 当前,似乎没有办法在不更改Autofac.DynamicProxy2集成模块的情况下提供此类挂钩。 Could be a nice addition to the InterceptedBy extension. 可能是InterceptedBy扩展的不错的补充。

Alternatively you could build the filtering into PerformanceInterceptor . 或者,您可以将过滤器内置到PerformanceInterceptor Looking at the IInvocation you're passed on invocation, examine the Method property to decide whether to profile or not. 查看您在调用时传递的IInvocation ,检查Method属性以确定是否进行概要分析。 But this will of course be less efficient than bypassing interception at the proxy level. 但是,这当然比在代理级别绕过侦听效率低。

For DynamicProxy2 , the EnableInterfaceInterceptors method now has an overload that takes a ProxyGenerationOptions object. 对于DynamicProxy2, EnableInterfaceInterceptors方法现在具有一个重载,该重载采用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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM