2

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

问题:除了选择性部分之外,我的一切都完美无缺。我可能是错的,但我认为我需要将我的拦截器与 IProxyGenerationHook 实现结合起来,但我不知道如何做到这一点。

我的代码看起来像这样:

被截取&剖析的接口(注意我只关心剖析Update()方法)

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

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

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

所有从容器中拉出的 ISomeSystemToMonitor 实例都会根据需要被拦截和分析,除了它会拦截其所有方法,而不仅仅是 Update 方法。

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

所有帮助表示赞赏,干杯!另外,请注意我现在无法升级到 autofac2.x;我被困在1。

4

2 回答 2

2

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

目前似乎没有一种方法可以在不更改 Autofac.DynamicProxy2 集成模块的情况下提供此类挂钩。可能是InterceptedBy扩展的一个很好的补充。

或者,您可以将过滤构建到PerformanceInterceptor. 查看IInvocation您在调用时传递的内容,检查Method属性以决定是否进行分析。但这当然比绕过代理级别的拦截效率低。

于 2010-04-19T08:32:42.543 回答
2

对于 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));
于 2016-07-22T18:11:52.487 回答