Castle.Aop.Autofac

https://github.com/mnoskovic/Castle.Aop

同样是基于dynamicproxy的,支持autofac

结果比snap靠谱

aop 1 before
aop 1 in
aop 2 in
real method runing
aop 2 out
aop 1 out
aop 1 after

顺序是希望的顺序

可以继承StandardInterceptor或直接实现IInterceptor

Interceptor(typeof(XXX))这个attribute做标记

autofac需要

var builder = new ContainerBuilder();
builder.RegisterModule(new InterceptorModule());
builder.RegisterType<Aop1>();
builder.RegisterType<Aop2>();
builder.RegisterType<TestHelper>().As<ITestHelper>();

1、注册InterceptorModule

2、注册aop类

3、注册真正的类

至少顺序比snap靠谱,性能未测试

看看在aop里改参数

aop 1 before
p is 1; r is (is null:True)
aop 1 in
p is 2; r is (is null:True)
aop 2 in
p is 3; r is (is null:True)
real method runing; r is 6
aop 2 out
p is 6; r is (is null:True)
aop 1 out
p is 7; r is (is null:True)
aop 1 after
p is 4; r is (is null:True)

方法调用传入1,aop1的before改为2,aop1 process前改为3,process后改为4,aop1 after改为5,aop2 process前改为6,后改为7。

实际传入的是6

去掉6,实际传入的是3;去掉3,实际传入的是2,去掉2,实际传入的是1,基本上还是符合预想的。

但是返回值就让我有点郁闷了,为什么都是null尼?方法执行完了还是null,这不扯呢嘛。而且外部调用确实也是没有收到返回值的。

原文地址:https://www.cnblogs.com/czcz1024/p/3119357.html