예제 #1
0
        private static string GetClassAndMethodName(IAsyncMethodInvocation methodInvocation)
        {
            var className = methodInvocation.TargetInstance.GetType().Name;

            var methodWithUserName = $"{className}={methodInvocation.MethodInfo.Name}=";

            return(methodWithUserName);
        }
예제 #2
0
        private static string GetParamWithValueKey(IAsyncMethodInvocation methodInvocation)
        {
            var childElementKey = string.Join(",", methodInvocation.Arguments
                                              .Select(x => new StringBuilder($"{x.ParameterInfo.Name}|{x.Value}")));

            return(childElementKey.IsNullOrWhiteSpace()
                ? GetClassAndMethodName(methodInvocation)
                : childElementKey);
        }
예제 #3
0
        private static async Task <IMethodInvocationResult> SetCacheAndReturnAsync(IAsyncMethodInvocation methodInvocation, IDictionary <string, object> useCache, string keyName)
        {
            var resultIn = await methodInvocation.InvokeNextAsync();

            //add to cache when it is a sucess call
            resultIn.Successful.WhenTrue(() => useCache.Add(keyName, resultIn.ReturnValue));

            return(resultIn);
        }
        public Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
        {
            var scope = _methodIdentityProvider.BeginNextMethodIdentityScope(_mehodIdentityKey);

            var res = methodInvocation.InvokeNextAsync();

            res.ContinueWith(t => scope.Dispose());

            return(res);
        }
            public async Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
            {
                var argument      = methodInvocation.GetArgument("value");
                var argumentValue = (int)argument.Value;

                if (argumentValue < 0)
                {
                    argument.Value = -argumentValue;
                }
                return(await methodInvocation.InvokeNextAsync().ConfigureAwait(false));
            }
            public async Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
            {
                var argument      = methodInvocation.GetArgument("value");
                var argumentValue = (int)argument.Value;

                int result;

                return(PrecalculatedValues.TryGetValue(argumentValue, out result)
                    ? methodInvocation.CreateResult(result)
                    : await methodInvocation.InvokeNextAsync().ConfigureAwait(false));
            }
예제 #7
0
        private static bool IsValidMethodCall(IAsyncMethodInvocation methodInvocation)
        {
            var result = new RuleStatement <bool>(
                () => methodInvocation.InstanceMethodInfo.ReturnType != typeof(void) &&
                methodInvocation.MethodInfo.GetCustomAttributes <ResolveFromCacheAttribute>() != null,
                new StopWithFuncRule <bool>(() => true), new StopWithFuncRule <bool>(() => false));

            var isValid = result.Process();

            return(isValid);
        }
예제 #8
0
        private async Task <IMethodInvocationResult> GetFromCacheAsync(IAsyncMethodInvocation methodInvocation)
        {
            var parentKey = GetClassAndMethodName(methodInvocation);

            var useCache = cache.Get <IDictionary <string, object> >(parentKey)
                           .NoneWhenNullOrDefault(() => ResolveCacheDictionary(parentKey));

            var keyName = GetParamWithValueKey(methodInvocation);

            return(useCache.TryGetValue(keyName, out object cacheEntry)
                ? methodInvocation.CreateResult(cacheEntry)
                : await SetCacheAndReturnAsync(methodInvocation, useCache, keyName));
        }
예제 #9
0
        public async Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
        {
            var cache = methodInvocation.MethodInfo.GetCustomAttributes(typeof(Cached), false).FirstOrDefault();

            if (cache != null)
            {
                var cacheObject = new MethodCacheObject(methodInvocation);

                var cacheManager = AppCore.Instance.Get <ICacheStrategy <MethodCacheObject> >();

                var cachedObject = cacheManager.MemoryCache.Get(cacheObject.Key).Result;

                if (null != cachedObject)
                {
                    if (DateTime.Now > cachedObject.Expiration)
                    {
                        cacheManager.MemoryCache.Remove(cachedObject.Key).Wait();
                    }
                    else
                    {
                        this.LogInformation($"{nameof(AsyncCachingInterceptor)} - {methodInvocation.MethodInfo.Name}");

                        return(methodInvocation.CreateResult(cachedObject.Value));
                    }
                }

                var result = await methodInvocation.InvokeNextAsync();

                cacheObject.Value = result.GetReturnValueOrThrow();

                if (null == cacheObject.Value)
                {
                    return(result);
                }

                cacheObject.Expiration = DateTime.Now.AddMilliseconds(((Cached)cache).Duration);

                await cacheManager.MemoryCache.Put(cacheObject.Key, cacheObject);

                return(result);
            }

            return(await methodInvocation.InvokeNextAsync());
        }
        public async Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
        {
            // BEFORE the target method execution
            var methodInput = CreateMethodInput(methodInvocation);

            var(msgInputLog, correlationState) = _methodLogMessageFactory.CreateInputMethodLogMessage(methodInput);

            _logger.Info(msgInputLog);

            try
            {
                // Yield to the next module in the pipeline
                var result = await methodInvocation.InvokeNextAsync().ConfigureAwait(false);

                // AFTER the target method execution
                var methodReturn = new MethodReturn {
                    ReturnValue = result.ReturnValue
                };

                if (methodInvocation.ActualReturnType.Name == "Void")
                {
                    methodReturn.ReturnValue = "Void";
                }

                var msgOutputLog = _methodLogMessageFactory.CreateOutputMethodLogMessage(methodReturn, correlationState);

                _logger.Info(msgOutputLog);

                return(result);
            }
            catch (Exception e)
            {
                var methodReturn = new MethodReturn {
                    Exception = e
                };

                var msgOutputLog = _methodLogMessageFactory.CreateOutputMethodLogMessage(methodReturn, correlationState);

                _logger.Error(msgOutputLog);

                throw;
            }
        }
            public async Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
            {
                if (methodInvocation.InstanceMethodInfo.Name != "IncrementAsync")
                {
                    return(await methodInvocation.InvokeNextAsync());
                }

                if (_invoked)
                {
                    throw new InvalidOperationException("Interceptor should be invoked once only");
                }
                _invoked = true;

                IMethodInvocationResult result = null;

                for (var i = 0; i < _retries; i++)
                {
                    result = await methodInvocation.InvokeNextAsync().ConfigureAwait(false);

                    await Task.Delay(50).ConfigureAwait(false);
                }

                return(result);
            }
 public async Task<IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
 {
     return await methodInvocation.InvokeNextAsync().ConfigureAwait(false);
 }
            public async Task<IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
            {
                var argument = methodInvocation.GetArgument("value");
                var argumentValue = (int)argument.Value;

                int result;
                return PrecalculatedValues.TryGetValue(argumentValue, out result)
                    ? methodInvocation.CreateResult(result)
                    : await methodInvocation.InvokeNextAsync().ConfigureAwait(false);
            }
 public async Task<IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
 {
     var argument = methodInvocation.GetArgument("value");
     var argumentValue = (int)argument.Value;
     if (argumentValue < 0)
     {
         argument.Value = -argumentValue;
     }
     return await methodInvocation.InvokeNextAsync().ConfigureAwait(false);
 }
예제 #15
0
 public Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
 {
     return(methodInvocation.InvokeNextAsync());
 }
예제 #16
0
 public async Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
 {
     return(await CallWithOrWithoutCache[IsValidMethodCall(methodInvocation).ToString()]
            .Invoke(methodInvocation));
 }
 public async Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
 {
     return(await methodInvocation.InvokeNextAsync().ConfigureAwait(false));
 }
예제 #18
0
 private async static Task <IMethodInvocationResult> GetWithoutCacheAsync(IAsyncMethodInvocation methodInvocation)
 => await methodInvocation.InvokeNextAsync();