protected override void AfterAction <T>(ref T val, IInvocation invocation) { var key = GetInvocationSignature(invocation); var att = invocation.GetCustomAttribute <CacheAttribute>(); _cacheProvider.Set(key, att.Region, val, att.Duration, att.Slide); // cacheAttr.Duration); }
public void Intercept(IInvocation invocation) { var attr = invocation.GetCustomAttribute <TAtt>(); if (attr == null) { invocation.Proceed(); return; } BeforeAction(invocation); if (invocation.ReturnValue != null) { return; } try { invocation.Proceed(); if (typeof(Task).IsAssignableFrom(invocation.Method.ReturnType)) { var x = InterceptAsync((dynamic)invocation.ReturnValue, invocation); invocation.ReturnValue = x;// InterceptAsync((dynamic)invocation.ReturnValue, invocation); } } catch (Exception ex) { CatchException(ex, invocation); } }
protected override void BeforeAction(IInvocation invocation) { var key = GetInvocationSignature(invocation); var method = invocation.MethodInvocationTarget; var att = invocation.GetCustomAttribute <CacheAttribute>(); var data = _cacheProvider.Get(key, att.Region); if (data == null) { return; } if (typeof(Task).IsAssignableFrom(method.ReturnType)) { //invocation.ReturnValue = ConvertAsync(data); var taskReturnType = method.ReturnType; //e.g. Task<int> var type = taskReturnType.GetGenericArguments()[0]; //get the result type, e.g. int var convertMethod = GetType().GetMethod(nameof(ConvertAsync), BindingFlags.Static | BindingFlags.NonPublic) ?.MakeGenericMethod(type); //Get the closed version of the Convert method, e.g. Convert<int> if (convertMethod != null) { try { invocation.ReturnValue = convertMethod.Invoke(null, new[] { data }); //Call the convert method and return the generic Task, e.g. Task<int> } catch (ArgumentException ex) { _logger.Exception(ex); return; } } return; } invocation.ReturnValue = data; }