public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            if (_cacheType.GetInterfaces().Contains(typeof(IDistributedCache)))
            {
            }

            //判断是否是异步方法
            bool isAsync = context.IsAsync();
            //if (context.ImplementationMethod.GetCustomAttribute(typeof(System.Runtime.CompilerServices.AsyncStateMachineAttribute)) != null)
            //{
            //    isAsync = true;
            //}
            //先判断方法是否有返回值,无就不进行缓存判断
            Type methodReturnType = context.GetReturnParameter().Type;

            if (methodReturnType == typeof(void) || methodReturnType == typeof(Task) || methodReturnType == typeof(ValueTask))
            {
                await next(context);

                return;
            }
            Type returnType = methodReturnType;

            if (isAsync)
            {
                //取得异步返回的类型
                returnType = returnType.GenericTypeArguments.FirstOrDefault();
            }
            //获取方法参数名
            //string param = CommonHelper.ObjectToJsonString(context.Parameters);
            ////获取方法名称,也就是缓存key值
            //string key = "Methods:" + context.ImplementationMethod.DeclaringType.FullName + "." + context.ImplementationMethod.Name;
            //var cache = context.ServiceProvider.GetService<ICacheHelper>();
        }
Beispiel #2
0
        /// <summary>
        /// 执行后
        /// </summary>
        private void ExecuteAfter(ILog log, AspectContext context, string methodName)
        {
            var parameter = context.GetReturnParameter();

            log.Caption($"{context.ServiceMethod.Name}方法执行后")
            .Method(methodName)
            .Content($"返回类型: {parameter.ParameterInfo.ParameterType.FullName},返回值: {parameter.Value.SafeString()}");
            WriteLog(log);
        }
Beispiel #3
0
        /// <summary>
        /// 执行后
        /// </summary>
        private void ExecuteAfter(ILog log, AspectContext context, string methodName)
        {
            var           parameter = context.GetReturnParameter();
            StringBuilder content   = new StringBuilder();

            content.AppendFormat($"{methodName}方法执行后返回:");
            parameter.AppendTo(content);
            WriteLog(log, content.ToString());
        }
Beispiel #4
0
        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var selector = (IParameterInterceptorSelector)context.ServiceProvider.GetService(typeof(IParameterInterceptorSelector));

            if (selector == null)
            {
                throw new InvalidOperationException("Cannot resolve ParameterInterceptorSelector.");
            }
            var parameters = context.GetParameters();
            var count      = parameters.Count;

            if (count > 0)
            {
                var parameterAspectInvoker = new ParameterAspectInvoker();
                for (var i = 0; i < count; i++)
                {
                    var parameter    = parameters[i];
                    var interceptors = selector.Select(parameter.ParameterInfo);
                    if (interceptors.Length > 0)
                    {
                        var parameterAspectContext = new ParameterAspectContext(context, parameter);
                        foreach (var interceptor in interceptors)
                        {
                            parameterAspectInvoker.AddDelegate(interceptor.Invoke);
                        }
                        await parameterAspectInvoker.Invoke(parameterAspectContext);

                        parameterAspectInvoker.Reset();
                    }
                }
            }
            await next(context);

            var returnParameter    = context.GetReturnParameter();
            var returnInterceptors = selector.Select(returnParameter.ParameterInfo);

            if (returnInterceptors.Length > 0)
            {
                var returnParameterAspectContext = new ParameterAspectContext(context, returnParameter);
                var returnParameterAspectInvoker = new ParameterAspectInvoker();
                foreach (var interceptor in returnInterceptors)
                {
                    returnParameterAspectInvoker.AddDelegate(interceptor.Invoke);
                }
                await returnParameterAspectInvoker.Invoke(returnParameterAspectContext);
            }
        }
Beispiel #5
0
        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var returnType = context.GetReturnParameter().Type;
            var hasCache   = context.ImplementationMethod.GetCustomAttributes(typeof(CacheAttribute), false).Any();

            if (returnType == typeof(void) ||
                returnType == typeof(Task) ||
                returnType == typeof(ValueTask) ||
                !hasCache)
            {
                await next(context);

                return;
            }

            if (context.IsAsync())
            {
                returnType = returnType.GenericTypeArguments.FirstOrDefault();
            }

            var cacheKey = this.CreateCacheKey(context);

            this.Log("trying to get", cacheKey);

            var cacheService = (ICacheService)context.ServiceProvider.GetService(typeof(ICacheService));

            var cachedResult = cacheService.Get(cacheKey);

            if (cachedResult == null)
            {
                this.Log("key not found", cacheKey);
                await next(context);

                var returnValue = context.IsAsync()
                    ? await context.UnwrapAsyncReturnValue()
                    : context.ReturnValue;

                this.Log("trying to set", cacheKey);
                if (!cacheService.Set(cacheKey, returnValue))
                {
                    this.Log("failed to set", cacheKey);
                }
            }
            else
            {
                this.Log("found key", cacheKey);
                if (context.IsAsync())
                {
                    if (context.ImplementationMethod.ReturnType == typeof(Task <>).MakeGenericType(returnType))
                    {
                        context.ReturnValue = typeof(Task)
                                              .GetMethod(nameof(Task.FromResult))
                                              .MakeGenericMethod(returnType)
                                              .Invoke(null, new[] { cachedResult });
                    }
                    if (context.ImplementationMethod.ReturnType == typeof(ValueTask <>).MakeGenericType(returnType))
                    {
                        context.ReturnValue = Activator
                                              .CreateInstance(typeof(ValueTask <>).MakeGenericType(returnType), cachedResult);
                    }
                }
                else
                {
                    context.ReturnValue = cachedResult;
                }
            }
        }