/// <summary>
        /// Initializes the exception context with current <see cref="MethodExecutedContext" />
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="executedContext"></param>
        public MethodExceptionContext(Exception exception, MethodExecutedContext executedContext)
        {
            Invocation = executedContext.Invocation;
            MethodInfo = executedContext.MethodInfo;
            InvocationContext = executedContext.InvocationContext;
            Exception = exception;

            _result = executedContext.Result;
            _updateResult = r => executedContext.Result = r;
        }
 public async Task OnExceptionAsync_should_call_OnException()
 {
     var context = new MethodExecutedContext(new MethodExecutingContext
     {
         InvocationContext = new Dictionary<string, object>()
     });
     var att = Substitute.ForPartsOf<ExceptionFilterAttribute>();
     
     await att.OnExceptionAsync(new MethodExceptionContext(new Exception(), context));
     att.Received(1).OnException(Arg.Any<MethodExceptionContext>());
     Assert.IsNull(context.Result);
 }
 public void SetUp()
 {
     Global.Init();
     Global.CacheStoreProvider.RegisterStore(new NoneExpireCacheStore());
     var invocation = Substitute.For<_IInvocation>();
     invocation.Method.Returns(typeof(RevalidateAttributeTests).GetMethod(nameof(SetUp)));
     _methodExecutedContext = new MethodExecutedContext(new MethodExecutingContext
     {
         InvocationContext = new Dictionary<string, object>(),
         Invocation = invocation
     });            
 }
        public virtual Task OnMethodExecutedAsync(MethodExecutedContext methodExecutedContext)
        {
            try
            {
                OnMethodExecuted(methodExecutedContext);
            }
            catch (Exception ex)
            {
                TaskHelpers.FromError(ex);
            }

            return TaskHelpers.DefaultCompleted;
        }
Example #5
0
        public void SetUp()
        {
            Global.Init();
            Global.CacheStoreProvider.RegisterStore(new NoneExpireCacheStore());
            var invocation = Substitute.For <_IInvocation>();

            invocation.Method.Returns(typeof(RevalidateAttributeTests).GetMethod(nameof(SetUp)));
            _methodExecutedContext = new MethodExecutedContext(new MethodExecutingContext
            {
                InvocationContext = new Dictionary <string, object>(),
                Invocation        = invocation
            });
        }
Example #6
0
        public async Task OnExceptionAsync_should_call_OnException()
        {
            var context = new MethodExecutedContext(new MethodExecutingContext
            {
                InvocationContext = new Dictionary <string, object>()
            });
            var att = Substitute.ForPartsOf <ExceptionFilterAttribute>();

            await att.OnExceptionAsync(new MethodExceptionContext(new Exception(), context));

            att.Received(1).OnException(Arg.Any <MethodExceptionContext>());
            Assert.IsNull(context.Result);
        }
        public void OnExceptionAsync_should_handle_error()
        {
            var context = new MethodExecutedContext(new MethodExecutingContext
            {
                InvocationContext = new Dictionary<string, object>()
            });
            var att = Substitute.ForPartsOf<ExceptionFilterAttribute>();
            att.When(x => x.OnException(Arg.Any<MethodExceptionContext>())).Do(c => { throw new Exception("Test exception"); } );
            var exceptionContext = new MethodExceptionContext(new Exception(), context);
            var t = att.OnExceptionAsync(exceptionContext);

            Assert.IsTrue(t.Status == TaskStatus.Faulted);
            Assert.AreEqual("Test exception", t.Exception.InnerExceptions[0].Message);
            Assert.IsNull(context.Result);
            Assert.IsNull(exceptionContext.Result);
        }
Example #8
0
        public void OnExceptionAsync_should_handle_error()
        {
            var context = new MethodExecutedContext(new MethodExecutingContext
            {
                InvocationContext = new Dictionary <string, object>()
            });
            var att = Substitute.ForPartsOf <ExceptionFilterAttribute>();

            att.When(x => x.OnException(Arg.Any <MethodExceptionContext>())).Do(c => { throw new Exception("Test exception"); });
            var exceptionContext = new MethodExceptionContext(new Exception(), context);
            var t = att.OnExceptionAsync(exceptionContext);

            Assert.IsTrue(t.Status == TaskStatus.Faulted);
            Assert.AreEqual("Test exception", t.Exception.InnerExceptions[0].Message);
            Assert.IsNull(context.Result);
            Assert.IsNull(exceptionContext.Result);
        }
 /// <summary>
 /// Pop the current trace and print info
 /// </summary>
 /// <param name="methodExecutedContext"></param>
 public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
 {
     var traces = GetTraceContexts();
     var trace = traces.Pop();
     trace.Timer.Stop();
     try
     {
         var invocation = methodExecutedContext.Invocation;
         trace.Info.Insert(0, $"{" ".PadLeft(trace.CallLevel * 4)} {(invocation.Method.DeclaringType ?? invocation.TargetType).FullName}.{invocation.Method.Name} : {trace.Timer.ElapsedMilliseconds}ms\r\n");
     }
     finally
     {
         if (!trace.IsNested)
         {
             Global.Logger.Info("\r\n" + trace.Info);
         }
     }
 }
Example #10
0
 public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
 {
     methodExecutedContext.InvocationContext[$"{nameof(FilterAttributeOnClassMethod)}.{nameof(OnMethodExecuted)}"] = DateTime.UtcNow;
 }
 public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
 {
     throw new Exception();
 }
 public override Task OnMethodExecutedAsync(MethodExecutedContext actionExecutedContext)
 {
     throw new Exception();
 }
 public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
 {
     throw new Exception();
 }
 /// <summary>
 /// Revalidate cache async
 /// </summary>
 /// <param name="methodExecutedContext"></param>
 /// <returns></returns>
 public override Task OnMethodExecutedAsync(MethodExecutedContext methodExecutedContext)
 {
     var revalidateKeys = KeyFormats.Select(k => CacheKeyProvider.GetRevalidateKey(methodExecutedContext.Invocation, k)).ToList();
     return Global.RevalidateCachesAsync(revalidateKeys);
 }
 public override Task OnMethodExecutedAsync(MethodExecutedContext actionExecutedContext)
 {
     throw new Exception($"{nameof(BadMethodFilterAttribute)}.{nameof(OnMethodExecutedAsync)}");
 }
 public override Task OnMethodExecutedAsync(MethodExecutedContext actionExecutedContext)
 {
     throw new Exception();
 }
        /// <summary>
        /// Save the return data from invocation to cache
        /// </summary>
        /// <param name="methodExecutedContext"></param>
        public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
        {
            if (Duration > 0 && !methodExecutedContext.TryGet<bool>(Global.__flatwhite_outputcache_restored))
            {
                var key = methodExecutedContext.TryGet<string>(Global.__flatwhite_outputcache_key);
                if (string.IsNullOrWhiteSpace(key))
                {
                    return;
                }

                var cacheStore = methodExecutedContext.TryGet<ICacheStore>(Global.__flatwhite_outputcache_store);
                var strategy = methodExecutedContext.TryGet<ICacheStrategy>(Global.__flatwhite_outputcache_strategy);
                var cacheItem = new CacheItem
                {
                    Key = key,
                    Data = methodExecutedContext.Result,
                    StoreId = cacheStore.StoreId,
                    StaleWhileRevalidate = StaleWhileRevalidate,
                    MaxAge = Duration,
                    CreatedTime = DateTime.UtcNow
                };
                CreatePhoenix(methodExecutedContext.Invocation, cacheItem);

                var changeMonitors = strategy.GetChangeMonitors(methodExecutedContext.Invocation, methodExecutedContext.InvocationContext);
                foreach (var mon in changeMonitors)
                {
                    mon.CacheMonitorChanged += x =>
                    {
                        RefreshCache(key);
                    };
                }

                cacheStore.Set(key, cacheItem, DateTime.UtcNow.AddSeconds(Duration + StaleWhileRevalidate));
            }
        }
 public override Task OnMethodExecutedAsync(MethodExecutedContext actionExecutedContext)
 {
     //Console.WriteLine($"{DateTime.Now} {nameof(TestMethod2FilterAttribute)} OnMethodExecutedAsync");
     return TaskHelpers.DefaultCompleted;
 }
 public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
 {
     //Console.WriteLine($"{DateTime.Now} {nameof(TestMethod2FilterAttribute)} OnMethodExecuted");
 }
Example #20
0
 /// <summary>
 /// Called after a service method is executed.
 /// </summary>
 /// <param name="serviceContext">The service context.</param>
 /// <param name="behaviorContext">The "method executed" behavior context.</param>
 public virtual void OnMethodExecuted(IServiceContext serviceContext, MethodExecutedContext behaviorContext)
 {
 }
 /// <summary>
 /// Clear the cache with the provided revalidation keys
 /// </summary>
 /// <param name="methodExecutedContext"></param>
 /// <returns></returns>
 public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
 {
     Global.RevalidateCaches(Keys);
 }
 public virtual void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
 {
 }
Example #23
0
 public override Task OnMethodExecutedAsync(MethodExecutedContext methodExecutedContext)
 {
     methodExecutedContext.InvocationContext[$"{nameof(FilterAttributeOnClassMethod)}.{nameof(OnMethodExecutedAsync)}"] = DateTime.UtcNow;
     return(Task.CompletedTask);
 }
 public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
 {
     //Console.WriteLine($"{DateTime.Now} {nameof(TestMethod2FilterAttribute)} OnMethodExecuted");
 }
 /// <summary>
 /// Revalidate cache async
 /// </summary>
 /// <param name="methodExecutedContext"></param>
 /// <returns></returns>
 public override Task OnMethodExecutedAsync(MethodExecutedContext methodExecutedContext)
 {
     return Global.RevalidateCachesAsync(Keys);
 }
 public override Task OnMethodExecutedAsync(MethodExecutedContext actionExecutedContext)
 {
     //Console.WriteLine($"{DateTime.Now} {nameof(TestMethod2FilterAttribute)} OnMethodExecutedAsync");
     return(TaskHelpers.DefaultCompleted);
 }
 public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
 {
     throw new Exception($"{nameof(BadMethodFilterAttribute)}.{nameof(OnMethodExecuted)}");
 }
 /// <summary>
 /// Clear the cache with the provided revalidation keys
 /// </summary>
 /// <param name="methodExecutedContext"></param>
 /// <returns></returns>
 public override void OnMethodExecuted(MethodExecutedContext methodExecutedContext)
 {
     var revalidatedKeys = KeyFormats.Select(k => CacheKeyProvider.GetRevalidateKey(methodExecutedContext.Invocation, k)).ToList();
     Global.RevalidateCaches(revalidatedKeys);
 }