autofac使用拦截器实现AOP,是基于Castle.Core的.然而Castle.Core并未提供原生异步支持.所以需要使用帮助类实现,这在autofac官方文档的已知问题中有详细说明:

https://autofaccn.readthedocs.io/en/latest/advanced/interceptors.html#asynchronous-method-interception

对于该问题的讨论,最早出现于stackoverflow:https://stackoverflow.com/questions/28099669/intercept-async-method-that-returns-generic-task-via-dynamicproxy

James Skimming基于其中的一个答案,研发了一个帮助包即: Castle.Core.AsyncInterceptor

我之前也一直使用的是该方案,不过thepirat000随后提出了一个使用dynamic的更加简化的实现方法:https://stackoverflow.com/a/39784559/7726468

我对其进行了一些封装,实现了一个新的帮助包,大家可以尝试一下,项目地址在:https://github.com/wswind/lightwind

使用时,你可以通过nuget安装Lightwind.Asyncinterceptor也可以直接拷贝AsyncInterceptorBase.cs放入你的项目中.样例代码可点击这里查看

其中的核心代码是封装实现一个支持异步处理的Interceptor,让开发者仅需关心实现所拦截的方法,在执行前后该添加什么处理逻辑.

核心源码如下:

    //inspired by : https://stackoverflow.com/a/39784559/7726468
public abstract class AsyncInterceptorBase : IInterceptor
{
public AsyncInterceptorBase()
{
}
public void Intercept(IInvocation invocation)
{
BeforeProceed(invocation);
invocation.Proceed();
if (IsAsyncMethod(invocation.MethodInvocationTarget))
{
invocation.ReturnValue = InterceptAsync((dynamic)invocation.ReturnValue, invocation);
}
else
{
AfterProceedSync(invocation);
}
} protected bool IsAsyncMethod(MethodInfo method)
{
var attr = method.GetCustomAttributes<AsyncStateMachineAttribute>(true);
bool isAsync = (attr != null) && typeof(Task).IsAssignableFrom(method.ReturnType);
return isAsync;
} private async Task InterceptAsync(Task task, IInvocation invocation)
{
await task.ConfigureAwait(false);
await AfterProceedAsync(invocation, false);
} protected object ProceedAsynResult { get; set; } private async Task<TResult> InterceptAsync<TResult>(Task<TResult> task, IInvocation invocation)
{
TResult result = await task.ConfigureAwait(false);
ProceedAsynResult = result;
await AfterProceedAsync(invocation,true);
return (TResult)ProceedAsynResult;
} protected virtual void BeforeProceed(IInvocation invocation) {} protected virtual void AfterProceedSync(IInvocation invocation) {} protected virtual Task AfterProceedAsync(IInvocation invocation,bool hasAsynResult)
{
return Task.CompletedTask;
}
}

异步拦截器的执行流程如下:

在所拦截的方法执行前,首先执行BeforeProceed,方法执行后,如果为同步方法,则后续执行AfterProceedSync

如果为异步方法,则await方法使其真正执行后,调用AfterProceedAsync.通过hasAsynResult可判断是否有返回值.

如果有返回值也可通过ProceedAsynResult读取或修改所拦截方法的最终返回值.

对于此问题的其他拦截器实现方法的代码样例,可参照代码:https://github.com/wswind/Learn-AOP/tree/master/AutofacAsyncInterceptor-More

.net core autofac asyncinterceptor 异步拦截器帮助包的更多相关文章

  1. .net core autofac asyncinterceptor 异步拦截器开发

    autofac使用拦截器实现AOP,是基于Castle.Core的.然而Castle.Core并未提供原生异步支持.所以需要使用帮助类实现,这在autofac官方文档的已知问题中有详细说明: http ...

  2. ASP.NET Core搭建多层网站架构【9.2-使用Castle.Core实现动态代理拦截器】

    2020/01/31, ASP.NET Core 3.1, VS2019, Autofac.Extras.DynamicProxy 4.5.0, Castle.Core.AsyncIntercepto ...

  3. ASP.NET Core 3.0 gRPC 拦截器

    目录 ASP.NET Core 3.0 使用gRPC ASP.NET Core 3.0 gRPC 双向流 ASP.NET Core 3.0 gRPC 拦截器 一. 前言 前面两篇文章给大家介绍了使用g ...

  4. 014-Spring Boot web【三】拦截器HandlerInterceptor、异常处理页面,全局异常处理ControllerAdvice

    一.拦截器HandlerInterceptor 1.1.HandlerInterceptor接口说明 preHandle,congtroller执行前,如果返回false请求终端 postHandle ...

  5. SpringMVC之八:基于SpringMVC拦截器和注解实现controller中访问权限控制

    SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法. preHandle在业务处理器 ...

  6. 源码解析Grpc拦截器(C#版本)

    前言 其实Grpc拦截器是我以前研究过,但是我看网上相关C#版本的源码解析相对少一点,所以笔者借这篇文章给大家分享下Grpc拦截器的实现,废话不多说,直接开讲(Grpc的源码看着很方便,包自动都能还原 ...

  7. struts2:拦截器

    拦截器(Interceptor)是Struts 2的核心组件,Struts 2框架的大部分功能都是通过拦截器来完成的,例如数据校验,国际化,文件上传和下载等.为了实现这些功能,Struts 2框架提供 ...

  8. Web系统Login拦截器

    所需要导入的包类:import org.springframework.web.servlet.HandleInterceptor;(拦截器要继承该类) public class loginInter ...

  9. 第十篇——Struts2的拦截器栈

    拦截器栈: 从结构上看:拦截器栈相当于多个拦截器的组合: 从功能上看:拦截器栈也是拦截器. 默认拦截器栈: 在struts-core.jar包中的struts-default.xml中自定义了一个de ...

随机推荐

  1. mariadb 3

    MariaDB第三章(select)   基本查询 --查询基本使用(条件,排序,聚合函数,分组,分页) --创建学生表 create table students ( id int unsigned ...

  2. 并发编程(四)Thread类详解

    一.引言 Thread类中存在着许多操作线程的方法,学习Thread类是非常有必要的,前面我们也嘘唏了创建线程的几种方式,若线程的创建不是以继承Thread类的方式创建的,那我们又改如何使用Threa ...

  3. Spring Boot学习(一)初识Spring Boot

    Spring Boot 概述 Spring Boot 是所有基于 Spring 开发的项目的起点.Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置 ...

  4. Spring与Junit测试整合

    一.引入spring测试包:text包 二.@RunWith:指定spring对junit提供的一个运行器 @ContextConfiguration:  locations指定spring配置文件位 ...

  5. Centos-显示文件类型-file

    file 长度为0的文件则显示为空位文件,对于软链接文件则显示链接的真实文件路径,默认输出会有文件名 相关选项 -b 只显示文件类型结果 -L 显示软链接指向文件的类型 -z 显示压缩文件信息 -i ...

  6. spark-3-macOS配置hadoop+spark+IDE

    [教程1]https://blog.csdn.net/shiyutianming/article/details/99946797  + [教程2]http://dblab.xmu.edu.cn/bl ...

  7. Python练习题 022:用递归函数反转字符串

    [Python练习题 022] 利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来. --------------------------------------- 又来一个递归题!不过,有 ...

  8. VUE第一个项目怎么读懂

    VUE介绍 VUE是前端开发框架. 原始的前端开发需要工程师写html.写css.写javascript(js).js是脚本语言,浏览器可以运行js来执行一些js支持的动作,例如点击反馈,下拉菜单.操 ...

  9. 我的Keras使用总结(5)——Keras指定显卡且限制显存用量,常见函数的用法及其习题练习

    Keras 是一个高层神经网络API,Keras是由纯Python编写而成并基于TensorFlow,Theano以及CNTK后端.Keras为支持快速实验而生,能够将我们的idea迅速转换为结果.好 ...

  10. C++库文件解析(conio.h)

    转载:https://blog.csdn.net/ykmzy/article/details/51276596 Conio.h 控制台输入输出库该文内容部分参照百度百科 Conio.h 在C stan ...