Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我想用AspectPointAttribute写一个方法自动存取缓存的AOP,当获得缓存值时,如何不运行方法直接返回结果。 #12

Closed
yus1977 opened this issue Aug 10, 2020 · 3 comments

Comments

@yus1977
Copy link

yus1977 commented Aug 10, 2020

我通过 invocation.ReturnValue来获得和返回原始方法的值,但好像不行
代码如下:

[AttributeUsage(AttributeTargets.Method,Inherited = true)]
 public class AutoCacheAbleAttribute : AspectPointAttribute
{
        public override async Task OnInvocation(AspectContext aspectContext, AspectDelegate _next)
        {
            ......
            ......
             var cacheValue = await ProcessGetAsync(invocation, methodInfo, returnType);
             //如果缓存存在,则直接返回
             if (cacheValue != null)
            {
                invocation.ReturnValue = cacheValue;
                return;
            }

            //执行原方法
            await _next(aspectContext);
            
            //缓存方法结果 
            await ProcessPutAsync(invocation, methodInfo, returnType, invocation.ReturnValue );
        }
}

我看了源代码。
在AopIntercept.cs里,方法的返回值是通过 aspectContext.Result 来传递,但是aspectContext.Result是internal的外部程序集是访问不到。
请问该如何处理。

@yuzd
Copy link
Owner

yuzd commented Aug 10, 2020

我看看

@yuzd yuzd closed this as completed in 8e8f9b3 Aug 10, 2020
@yuzd
Copy link
Owner

yuzd commented Aug 10, 2020

我提交了新版。你拿新版 好像代码都不用动,应该就可以实现你的想法。
也 可以看下 单元测试 8e8f9b3

@yus1977
Copy link
Author

yus1977 commented Aug 11, 2020

多谢!!
经过测试,OK了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants