Exemplo n.º 1
0
        public override async Task InterceptAsync(IRocketMethodInvocation invocation)
        {
            if (RocketCrossCuttingConcerns.IsApplied(invocation.TargetObject, RocketCrossCuttingConcerns.FeatureChecking))
            {
                await invocation.ProceedAsync();

                return;
            }

            await CheckFeaturesAsync(invocation);

            await invocation.ProceedAsync();
        }
        public override async Task InterceptAsync(IRocketMethodInvocation invocation)
        {
            var parameters = invocation.Method.GetParameters();

            //判断Method是否包含ref / out参数
            if (parameters.Any(it => it.IsIn || it.IsOut))
            {
                await invocation.ProceedAsync();
            }
            else
            {
                var distributedCacheAttribute = ReflectionHelper.GetSingleAttributeOrDefault <LocalCacheAttribute> (invocation.Method);
                var cacheKey   = CacheKeyHelper.GetCacheKey(invocation.Method, invocation.Arguments, distributedCacheAttribute.Prefix);
                var returnType = invocation.Method.ReturnType.GetGenericArguments().First();
                try {
                    var cacheValue = await _cache.GetAsync(cacheKey);

                    if (cacheValue != null)
                    {
                        var resultValue = _serializer.Deserialize(cacheValue, returnType);
                        invocation.ReturnValue = invocation.IsAsync() ? _taskResultMethod.MakeGenericMethod(returnType).Invoke(null, new object[] { resultValue }) : resultValue;
                    }
                    else
                    {
                        if (!distributedCacheAttribute.ThreadLock)
                        {
                            await GetResultAndSetCache(invocation, cacheKey, distributedCacheAttribute.Expiration);
                        }
                        else
                        {
                            using (await _lock.LockAsync()) {
                                cacheValue = await _cache.GetAsync(cacheKey);

                                if (cacheValue != null)
                                {
                                    var resultValue = _serializer.Deserialize(cacheValue, returnType);
                                    invocation.ReturnValue = invocation.IsAsync() ? _taskResultMethod.MakeGenericMethod(returnType).Invoke(null, new object[] { resultValue }) : resultValue;
                                }
                                else
                                {
                                    await GetResultAndSetCache(invocation, cacheKey, distributedCacheAttribute.Expiration);
                                }
                            }
                        }
                    }
                } catch (Exception) {
                    await invocation.ProceedAsync();
                }
            }
        }
Exemplo n.º 3
0
        public override async Task InterceptAsync(IRocketMethodInvocation invocation)
        {
            if (!UnitOfWorkHelper.IsUnitOfWorkMethod(invocation.Method, out var unitOfWorkAttribute))
            {
                await invocation.ProceedAsync();

                return;
            }

            using (var uow = _unitOfWorkManager.Begin(CreateOptions(invocation, unitOfWorkAttribute))) {
                await invocation.ProceedAsync();

                await uow.CompleteAsync();
            }
        }
 /// <summary>
 /// 直接调用方法,并把结果加入缓存
 /// </summary>
 /// <param name="context"></param>
 /// <param name="next"></param>
 /// <param name="key">缓存key</param>
 /// <param name="type">缓存值类型</param>
 /// <returns></returns>
 public async Task GetResultAndSetCache(IRocketMethodInvocation invocation, string cacheKey, long expiration)
 {
     await invocation.ProceedAsync().ContinueWith(task => {
         if (invocation.ReturnValue != null)
         {
             _cache.SetAsync(cacheKey, _serializer.Serialize(invocation.ReturnValue), new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(expiration)));
         }
     });
 }
Exemplo n.º 5
0
 /// <summary>
 /// 直接调用方法,并把结果加入缓存
 /// </summary>
 /// <param name="context"></param>
 /// <param name="next"></param>
 /// <param name="key">缓存key</param>
 /// <param name="type">缓存值类型</param>
 /// <returns></returns>
 public async Task GetResultAndSetCache(IRocketMethodInvocation invocation, string cacheKey, long expiration)
 {
     await invocation.ProceedAsync().ContinueWith(task => {
         if (invocation.ReturnValue != null)
         {
             _cache.Set(cacheKey, invocation.ReturnValue, TimeSpan.FromMinutes(expiration));
         }
     });
 }
Exemplo n.º 6
0
        public override async Task InterceptAsync(IRocketMethodInvocation invocation)
        {
            if (!ShouldIntercept(invocation, out var auditLog, out var auditLogAction))
            {
                await invocation.ProceedAsync();

                return;
            }

            var stopwatch = Stopwatch.StartNew();

            try {
                await invocation.ProceedAsync();
            } catch (Exception ex) {
                auditLog.Exceptions.Add(ex);
                throw;
            } finally {
                stopwatch.Stop();
                auditLogAction.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds);
                auditLog.Actions.Add(auditLogAction);
            }
        }
Exemplo n.º 7
0
 public override async Task InterceptAsync(IRocketMethodInvocation invocation)
 {
     Validate(invocation);
     await invocation.ProceedAsync();
 }
Exemplo n.º 8
0
        public override async Task InterceptAsync(IRocketMethodInvocation invocation)
        {
            await AuthorizeAsync(invocation);

            await invocation.ProceedAsync();
        }