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

发布于 2024-08-29 00:05:16 字数 1103 浏览 6 评论 0原文

我目前正在使用 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困住了。

I'm currently doing a bit of experimenting using Autofac-1.4.5.676, autofac contrib and 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.

My code looks something like this:

The interface that is to be intercepted & profiled (note that I only care about profiling the Update() method)

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.

Now, how can I extend this to exclude all methods other than 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".

All help appreciated, cheers! Also, please note that I can't upgrade to autofac2.x right now; I'm stuck with 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2024-09-05 00:05:16

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

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

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

A IProxyGenerationHook instance have to be passed to the CreateInterfaceProxyWithTarget call when the interceptor is produced. 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. Could be a nice addition to the InterceptedBy extension.

Alternatively you could build the filtering into PerformanceInterceptor. Looking at the IInvocation you're passed on invocation, examine the Method property to decide whether to profile or not. But this will of course be less efficient than bypassing interception at the proxy level.

旧瑾黎汐 2024-09-05 00:05:16

对于 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));

For DynamicProxy2 , the EnableInterfaceInterceptors method now has an overload that takes a ProxyGenerationOptions object.

//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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文