Contains information for the executing action.
 internal MethodExecutedContext(MethodExecutingContext executingContext)
 {
     Invocation        = executingContext.Invocation;
     MethodInfo        = executingContext.MethodInfo;
     InvocationContext = new Dictionary <string, object>(executingContext.InvocationContext);
     Result            = executingContext.Result;
 }
Example #2
0
        /// <summary>
        /// Check if there is a parent trace, create a new trace and push to the stack which is stored in the CallContext
        /// </summary>
        /// <param name="methodExecutingContext"></param>
        public override void OnMethodExecuting(MethodExecutingContext methodExecutingContext)
        {
            var traces = GetTraceContexts();
            var trace  = traces.Count > 0 ? new TraceContext(traces.Peek()) : new TraceContext();

            traces.Push(trace);
        }
 /// <summary>
 /// Initializes the exception context with current <see cref="MethodExecutingContext" />
 /// </summary>
 /// <param name="exception"></param>
 /// <param name="executingContext"></param>
 public MethodExceptionContext(Exception exception, MethodExecutingContext executingContext)
 {
     Invocation = executingContext.Invocation;
     MethodInfo = executingContext.MethodInfo;
     InvocationContext = executingContext.InvocationContext;
     Exception = exception;
     _updateResult = r => executingContext.Result = r;
 }
 /// <summary>
 /// Initializes the exception context with current <see cref="MethodExecutingContext" />
 /// </summary>
 /// <param name="exception"></param>
 /// <param name="executingContext"></param>
 public MethodExceptionContext(Exception exception, MethodExecutingContext executingContext)
 {
     Invocation        = executingContext.Invocation;
     MethodInfo        = executingContext.MethodInfo;
     InvocationContext = executingContext.InvocationContext;
     Exception         = exception;
     _updateResult     = r => executingContext.Result = r;
 }
        public virtual Task OnMethodExecutingAsync(MethodExecutingContext methodExecutingContext)
        {
            try
            {
                OnMethodExecuting(methodExecutingContext);
            }
            catch (Exception ex)
            {
                TaskHelpers.FromError(ex);
            }

            return TaskHelpers.DefaultCompleted;
        }
Example #6
0
        public virtual Task OnMethodExecutingAsync(MethodExecutingContext methodExecutingContext)
        {
            try
            {
                OnMethodExecuting(methodExecutingContext);
            }
            catch (Exception ex)
            {
                return(TaskHelpers.FromError(ex));
            }

            return(TaskHelpers.DefaultCompleted);
        }
Example #7
0
        private void HandleSync(List <MethodFilterAttribute> filterAttributes, MethodExecutingContext methodExecutingContext)
        {
            Exception exception = null;

            foreach (var f in filterAttributes)
            {
                try
                {
                    if (methodExecutingContext.Result == null)
                    {
                        f.OnMethodExecuting(methodExecutingContext);
                    }
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                HandleException(new MethodExceptionContext(exception, methodExecutingContext));
                exception = null;
            }

            filterAttributes.Reverse();
            var methodExecutedContext = new MethodExecutedContext(methodExecutingContext);

            foreach (var f in filterAttributes)
            {
                try
                {
                    f.OnMethodExecuted(methodExecutedContext);
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                HandleException(new MethodExceptionContext(exception, methodExecutedContext));
            }
            if (methodExecutedContext.Result != null && typeof(void) != methodExecutedContext.Invocation.Method.ReturnType)
            {
                methodExecutedContext.Invocation.ReturnValue = methodExecutedContext.Result;
            }
        }
        public override Task OnMethodExecutingAsync(MethodExecutingContext actionContext)
        {
            _invocation.Proceed();

            var taskResult = _invocation.ReturnValue as Task;

            if (taskResult != null && _taskGenericReturnType != null)
            {
                var result = _invocation.Method.ReturnType.GetProperty("Result").GetValue(taskResult, null);
                actionContext.Result = result;
            }

            return taskResult;
        }
Example #9
0
        /// <summary>
        /// Intercept the invocation
        /// </summary>
        /// <param name="invocation"></param>
        public void Intercept(_IInvocation invocation)
        {
            if (!invocation.Method.IsVirtual || invocation.Method.IsFinal)
            {
                invocation.Proceed();
                return;
            }

            var methodExecutingContext = new MethodExecutingContext
            {
                InvocationContext = _contextProvider.GetContext(),
                MethodInfo        = invocation.Method,
                Invocation        = invocation
            };

            var attributes = GetInvocationMethodFilterAttributes(invocation, methodExecutingContext.InvocationContext);

            if (attributes.Any(a => a is NoInterceptAttribute))
            {
                invocation.Proceed();
                return;
            }

            var filterAttributes = attributes.OfType <MethodFilterAttribute>().OrderBy(x => x.Order).ToList();
            var isAsync          = typeof(Task).IsAssignableFrom(invocation.Method.ReturnType);

            if (isAsync)
            {
                if (invocation.Method.ReturnType.IsGenericType && invocation.Method.ReturnType.GetGenericTypeDefinition() == typeof(Task <>))
                {
                    var taskResultType = invocation.Method.ReturnType.GetGenericArguments()[0];
                    var mInfo          = _handleAsyncWithTypeMethod.MakeGenericMethod(taskResultType);
                    filterAttributes.Add(new InvocationAttribute(invocation, taskResultType));
                    invocation.ReturnValue = mInfo.Invoke(this, new object[] { filterAttributes, methodExecutingContext, taskResultType });
                }
                else
                {
                    filterAttributes.Add(new InvocationAttribute(invocation));
                    invocation.ReturnValue = HandleAsync(filterAttributes, methodExecutingContext);
                }
            }
            else
            {
                filterAttributes.Add(new InvocationAttribute(invocation));
                HandleSync(filterAttributes, methodExecutingContext);
            }
        }
Example #10
0
        public override async Task OnMethodExecutingAsync(MethodExecutingContext actionContext)
        {
            _invocation.Proceed();

            var taskResult = _invocation.ReturnValue as Task;

            if (taskResult != null)
            {
                if (_taskGenericReturnType != null)
                {
                    actionContext.Result = await taskResult.TryGetTaskResult();
                }
                else
                {
                    await taskResult;
                }
            }
            else
            {
                actionContext.Result = _invocation.ReturnValue;
            }
        }
        public override async Task OnMethodExecutingAsync(MethodExecutingContext actionContext)
        {
            _invocation.Proceed();

            var taskResult = _invocation.ReturnValue as Task;

            if (taskResult != null)
            {
                if (_taskGenericReturnType != null)
                {
                    actionContext.Result = await taskResult.TryGetTaskResult();
                }
                else
                {
                    await taskResult;
                }
            }
            else
            {
                actionContext.Result = _invocation.ReturnValue;
            }
        }
Example #12
0
        /// <summary>
        /// Check to see if the cache is available
        /// </summary>
        /// <param name="methodExecutingContext"></param>
        public override void OnMethodExecuting(MethodExecutingContext methodExecutingContext)
        {
            var strategy = _cacheStrategy ?? Global.CacheStrategyProvider.GetStrategy(methodExecutingContext.Invocation, methodExecutingContext.InvocationContext);

            if (Duration <= 0 || strategy == null || !strategy.CanCache(methodExecutingContext.Invocation, methodExecutingContext.InvocationContext))
            {
                return;
            }
            methodExecutingContext.InvocationContext[Global.__flatwhite_outputcache_attribute] = this;

            var key        = strategy.CacheKeyProvider.GetCacheKey(methodExecutingContext.Invocation, methodExecutingContext.InvocationContext);
            var cacheStore = strategy.GetCacheStore(methodExecutingContext.Invocation, methodExecutingContext.InvocationContext);

            var cacheItem = cacheStore.Get(key) as CacheItem;

            if (cacheItem != null)
            {
                methodExecutingContext.Result = cacheItem.Data;
                methodExecutingContext.InvocationContext[Global.__flatwhite_outputcache_restored] = true;

                if (cacheItem.IsStale())
                {
                    if (!Global.Cache.PhoenixFireCage.ContainsKey(key))
                    {
                        //If this is the first request but the "cacheItem" (Possibly distributed cache item" has the cache
                        CreatePhoenix(methodExecutingContext.Invocation, cacheItem);
                    }
                    RefreshCache(key);
                }

                return;
            }
            Global.Logger.Info($"Cache is not available for key {key}");
            methodExecutingContext.InvocationContext[Global.__flatwhite_outputcache_store]    = cacheStore;
            methodExecutingContext.InvocationContext[Global.__flatwhite_outputcache_key]      = key;
            methodExecutingContext.InvocationContext[Global.__flatwhite_outputcache_strategy] = strategy;
        }
 public virtual void OnMethodExecuting(MethodExecutingContext methodExecutingContext)
 {
 }
 /// <summary>
 /// Check if there is a parent trace, create a new trace and push to the stack which is stored in the CallContext
 /// </summary>
 /// <param name="methodExecutingContext"></param>
 public override void OnMethodExecuting(MethodExecutingContext methodExecutingContext)
 {
     var traces = GetTraceContexts();
     var trace = traces.Count > 0 ? new TraceContext(traces.Peek()) : new TraceContext();
     traces.Push(trace);
 }
        /// <summary>
        /// Check to see if the cache is available
        /// </summary>
        /// <param name="methodExecutingContext"></param>
        public override void OnMethodExecuting(MethodExecutingContext methodExecutingContext)
        {
            var strategy = _cacheStrategy ?? Global.CacheStrategyProvider.GetStrategy(methodExecutingContext.Invocation, methodExecutingContext.InvocationContext);
            if (Duration <= 0 || strategy == null || !strategy.CanCache(methodExecutingContext.Invocation, methodExecutingContext.InvocationContext))
            {
                return;
            }
            methodExecutingContext.InvocationContext[Global.__flatwhite_outputcache_attribute] = this;

            var key = strategy.CacheKeyProvider.GetCacheKey(methodExecutingContext.Invocation, methodExecutingContext.InvocationContext);
            var cacheStore = strategy.GetCacheStore(methodExecutingContext.Invocation, methodExecutingContext.InvocationContext);

            var cacheItem = cacheStore.Get(key) as CacheItem;
            if (cacheItem != null)
            {
                methodExecutingContext.Result = cacheItem.Data;
                methodExecutingContext.InvocationContext[Global.__flatwhite_outputcache_restored] = true;

                if (cacheItem.Age > cacheItem.MaxAge)
                {
                    if (!Global.Cache.PhoenixFireCage.ContainsKey(key))
                    {
                        //If this is the first request but the "cacheItem" (Possibly distributed cache item" has the cache
                        CreatePhoenix(methodExecutingContext.Invocation, cacheItem);
                    }
                    if (!AutoRefresh)
                    {
                        RefreshCache(key);
                    }
                }

                return;
            }
            Global.Logger.Info($"Cache is not available for key {key}");
            methodExecutingContext.InvocationContext[Global.__flatwhite_outputcache_store] = cacheStore;
            methodExecutingContext.InvocationContext[Global.__flatwhite_outputcache_key] = key;
            methodExecutingContext.InvocationContext[Global.__flatwhite_outputcache_strategy] = strategy;
        }
Example #16
0
 public virtual void OnMethodExecuting(MethodExecutingContext methodExecutingContext)
 {
 }
Example #17
0
        /// <summary>
        /// This will be called via Reflection
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="filterAttributes"></param>
        /// <param name="methodExecutingContext"></param>
        /// <param name="taskResultType"></param>
        /// <returns></returns>
        private async Task <TResult> HandleAsyncWithType <TResult>(List <MethodFilterAttribute> filterAttributes, MethodExecutingContext methodExecutingContext, Type taskResultType)
        {
            Exception exception = null;

            foreach (var f in filterAttributes)
            {
                try
                {
                    if (methodExecutingContext.Result == null)
                    {
                        await f.OnMethodExecutingAsync(methodExecutingContext).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                await HandleExceptionAsync(new MethodExceptionContext(exception, methodExecutingContext));

                exception = null;
            }

            filterAttributes.Reverse();
            var methodExecutedContext = new MethodExecutedContext(methodExecutingContext);

            foreach (var f in filterAttributes)
            {
                try
                {
                    await f.OnMethodExecutedAsync(methodExecutedContext).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                await HandleExceptionAsync(new MethodExceptionContext(exception, methodExecutedContext));
            }

            return(methodExecutedContext.Result != null ? (TResult)methodExecutedContext.Result : default(TResult));
        }
 public override void OnMethodExecuting(MethodExecutingContext methodExecutingContext)
 {
     _invocation.Proceed();
     methodExecutingContext.Result = _invocation.ReturnValue;
 }
Example #19
0
        private async Task HandleAsync(List <MethodFilterAttribute> filterAttributes, MethodExecutingContext methodExecutingContext)
        {
            Exception exception = null;

            foreach (var f in filterAttributes)
            {
                try
                {
                    await f.OnMethodExecutingAsync(methodExecutingContext).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                await HandleExceptionAsync(new MethodExceptionContext(exception, methodExecutingContext));

                exception = null;
            }

            filterAttributes.Reverse();
            var methodExecutedContext = new MethodExecutedContext(methodExecutingContext);

            foreach (var f in filterAttributes)
            {
                try
                {
                    await f.OnMethodExecutedAsync(methodExecutedContext).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    exception = ex;
                    break;
                }
            }

            if (exception != null)
            {
                await HandleExceptionAsync(new MethodExceptionContext(exception, methodExecutedContext));
            }
        }
Example #20
0
 public override void OnMethodExecuting(MethodExecutingContext methodExecutingContext)
 {
     _invocation.Proceed();
     methodExecutingContext.Result = _invocation.ReturnValue;
 }