【发布时间】:2013-10-01 06:29:21
【问题描述】:
我正在使用 Ninject 和 AOP 来做一些缓存。我有一个属性,我可以将其应用于存储库中的任何方法,并且在 BeforeInvoke 上,如果有一个缓存对象,它将返回我的缓存对象,并且 AfterInvoke 创建一个缓存对象。这一切都很好,但我不知道如何停止调用初始方法,即如果有一个缓存的对象,则返回它而不是调用一个被拦截的方法。我的拦截器在这里:
public class CacheInterceptor : SimpleInterceptor
{
protected override void BeforeInvoke(IInvocation invocation)
{
Type returnType = invocation.Request.Method.ReturnType;
string cacheKey = CacheKeyBuilder.GetCacheKey(invocation, serializer);
object cachedValue = cache.Get(cacheKey);
if (cachedValue == null)
{
invocation.Proceed();
}
else
{
object returnValue = serializer.Deserialize(returnType, cachedValue);
invocation.ReturnValue = returnValue;
returnedCachedResult = true;
}
}
}
尽管在 else 语句中我显然不是说调用被调用的方法'invocation.Proceed();'它仍然调用它。如何告诉 ninject 只返回 invocation.ReturnValue ?
【问题讨论】:
-
你确定你的拦截器被调用了吗?你能在调试器中单步执行吗?
-
是的,拦截器正在被调用,我可以看到调用。ReturnValue = returnValue;被设置但是它也调用了方法
标签: c# ninject aop interception ninject-interception