public override Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { var t = Task.FromResult((int)context.Parameters[0]); context.ReturnValue = t; return(t); }
public static AsyncAspectDelegate ConverTotReturnValueTask <T>(AsyncAspectDelegate aspectDelegate) { return(c => { var task = CallValueTask <T>(aspectDelegate(c), c); c.ReturnTask = new ValueTask <T>(task); return task; }); }
public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { if ((bool)context.Parameters[0]) { await next(context); } else { throw new FieldAccessException(); } }
public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { await next(context); var v = context.ReturnValue; if (v is Task <string> t) { context.ReturnValue = Task.FromResult(t.Result + "A"); } }
public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { try { await next(context); } catch (Exception ex) { context.SetException(ex); await interceptor.InvokeAsync(context, next); } }
public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { // 整个代码基本和我们直接使用 MemoryCache 一样 var cache = context.ServiceProvider.GetRequiredService <IMemoryCache>(); var r = await cache.GetOrCreateAsync(cacheKey, async e => { await next(context); // 所以真正实现的方法逻辑都在 next 中,所以调用它就好了 e.AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow; return(context.ReturnValue); // 结果都在ReturnValue , 这里为了简单,就不写 void / Task<T> / ValueTask<T> 等等 各种返回值的兼容代码了 }); context.ReturnValue = r; // 设置 ReturnValue, 由于缓存有效期内, next不会被调用, 所以ReturnValue不会有值,我们需要将缓存结果设置到 ReturnValue }
public AsyncAspectDelegate GetInterceptorAsync(MethodInfo method) { AsyncAspectDelegate baseCall = method.IsGenericMethodDefinition ? async c => { await asyncGenericCallers.GetOrAdd(c.Method, serviceMethod => { var m = method.MakeGenericMethod(serviceMethod.GetGenericArguments()); return(CreateAsyncCaller(m)); })(c); } : CreateAsyncCaller(method); return(CreateAsyncAspectDelegate(method, baseCall)); }
public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { var caches = context.ServiceProvider.GetRequiredService <IEnumerable <ICacheAdapter> >() .ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase); var cas = context.Method.GetReflector() .GetCustomAttributes <CacheAttribute>() .OrderBy(i => i.Order) .ToArray(); // 为了简单,我们就使用最简单的反射形式调用 var m = typeof(CacheInterceptor).GetMethod(nameof(CacheInterceptor.GetOrCreateAsync)) .MakeGenericMethod(context.Method.ReturnType.GetGenericArguments()[0]); await(Task) m.Invoke(this, new object[] { caches, cas, context, next, 0 }); }
public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { await asyncCache.GetOrAdd(context.Method, CreateHttpClientCaller)(context); }
private AsyncAspectDelegate CreateAsyncAspectDelegate(MethodInfo serviceMethod, AsyncAspectDelegate baseCall) { return(FindInterceptors(serviceMethod).Select(i => { CallAsyncAspectDelegate a = i.InvokeAsync; return a; }).Aggregate(baseCall, (i, j) => c => j(c, i))); }
public async Task <T> GetOrCreateAsync <T>(Dictionary <string, ICacheAdapter> adapters, CacheAttribute[] options, AspectContext context, AsyncAspectDelegate next, int index) { if (index >= options.Length) { Console.WriteLine($"No found Cache at {DateTime.Now}."); // 所有cache 都找完了,没有找到有效cache,所以需要拿真正的结果 await next(context); // 为了简单,我们就只支持 Task<T> 的结果 return(((Task <T>)context.ReturnValue).Result); } var op = options[index]; T result; var cacheName = op.CacheName; if (adapters.TryGetValue(cacheName, out var adapter)) { if (!adapter.TryGetValue <T>(op.CacheKey, out result)) { // 当前缓存找不到结果,移到下一个缓存获取结果 result = await GetOrCreateAsync <T>(adapters, options, context, next, ++index); adapter.Set(op.CacheKey, result, TimeSpan.Parse(op.Ttl)); // 更新当前缓存实现的存储 context.ReturnValue = Task.FromResult(result); // 为了简单,我们就在这儿更新返回结果,其实不该在这儿的,为什么,大家可以猜一猜为什么? } else { Console.WriteLine($"Get Cache From {cacheName} at {DateTime.Now}."); context.ReturnValue = Task.FromResult(result); // 为了简单,我们就在这儿更新返回结果,其实不该在这儿的,为什么,大家可以猜一猜为什么? } } else { throw new ArgumentException($"No such cache: {cacheName}."); } return(result); }
public abstract Task InvokeAsync(AspectContext context, AsyncAspectDelegate next);
public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { await asyncCache.GetOrAdd(context.Method, CreateAsyncPolicy)(context, next); }
private static AsyncAspectDelegate CreateTaskExceptionConvertor(MethodInfo serviceMethod, AsyncAspectDelegate aspectDelegate) { if (serviceMethod.IsReturnTask()) { var type = serviceMethod.ReturnType.GetGenericArguments()[0]; return(Constants.ConverTotReturnTask.MakeGenericMethod(type) .Invoke(null, new object[] { aspectDelegate }) as AsyncAspectDelegate); } else if (serviceMethod.IsReturnValueTask()) { var type = serviceMethod.ReturnType.GetGenericArguments()[0]; return(Constants.ConverTotReturnValueTask.MakeGenericMethod(type) .Invoke(null, new object[] { aspectDelegate }) as AsyncAspectDelegate); } else { return(aspectDelegate); } }
private AsyncAspectDelegate CreateAsyncAspectDelegate(MethodInfo serviceMethod, AsyncAspectDelegate baseCall) { var interceptor = FindInterceptors(serviceMethod).Select(i => { CallAsyncAspectDelegate a = i.InvokeAsync; return(a); }).Aggregate(baseCall, (i, j) => c => j(c, i)); return(CreateTaskExceptionConvertor(serviceMethod, interceptor)); }
public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { Console.WriteLine($"{context.Service.GetType().GetReflector().FullDisplayName}.{context.Method.GetReflector().DisplayName}"); await next(context); }
public static AsyncAspectDelegate ConverTotReturnTask <T>(AsyncAspectDelegate aspectDelegate) { return(c => Call <T>(aspectDelegate(c), c)); }
public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { InjectParameters(context); await next(context); }
public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next) { await next(context); AddTen(context); }