Esempio n. 1
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);
        }
Esempio n. 2
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 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;

                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));
            }
            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)
        {
            // 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)
 {
     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);
 }
 public async Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
 {
     return(await methodInvocation.InvokeNextAsync().ConfigureAwait(false));
 }
Esempio n. 12
0
 private async static Task <IMethodInvocationResult> GetWithoutCacheAsync(IAsyncMethodInvocation methodInvocation)
 => await methodInvocation.InvokeNextAsync();
Esempio n. 13
0
 public Task <IMethodInvocationResult> InterceptAsync(IAsyncMethodInvocation methodInvocation)
 {
     return(methodInvocation.InvokeNextAsync());
 }