public override async Task Invoke(AspectContext context, AspectDelegate next) { String cacheKey = null; var logger = context.ServiceProvider.GetRequiredService <ILogger <MemcacheDoBeforeAttribute> >(); try { //去Memcached中看看有没有我们的数据 命名空间+ 类名 + 方法名 + 参数(多个) cacheKey = getCacheKey(context); logger.LogInformation($"cacheKey:{cacheKey}"); var returnType = context.ServiceMethod.ReturnType; bool isTask = false; if (returnType.GetProperty("Result") != null) { isTask = true; returnType = returnType.GetProperty("Result").PropertyType; } //如果Memcached 连接不上呢 if (!CachingProvider.IsConnect) { logger.LogError("Memcached服务器可能不存在或是连接不上"); await next(context); return; } var cacheValue = CachingProvider.GetStringAsync(cacheKey).Result; //返回值 if (string.IsNullOrEmpty(cacheValue)) { //回Service await next(context); var vulue = context.ReturnValue; if (isTask) { vulue = context.ReturnValue.GetType().GetProperty("Result").GetValue(context.ReturnValue, null); } var values = JsonConvert.SerializeObject(vulue); //先放到Memcached中一份 await CachingProvider.SetStringAsync(cacheKey, values, TimeSpan.FromSeconds(expiry)); cacheValue = values; } Console.WriteLine("Before service call"); var value = JsonConvert.DeserializeObject(cacheValue, returnType); if (isTask) { context.ReturnValue = value; await context.Complete(); } else { context.ReturnValue = value; } } catch (Exception) { } finally { } }