dotnetcore实现Aop
dotnetcore实现Aop
Aop大家都不陌生,然而今天给大家不将讲官方的filter,今天给大家分享一个轻量级的Aop解决方案(AspectCore)
什么是AspectCore
AspectCore是一个面向切面编程,基于.NetCore和.NetFramwork的扩平台框架,对方法拦截器、依赖项注入集成、web应用程序、数据验证等提供核心支持。
AspectCore基本特性
提供抽象的Aop接口,基于该接口可以轻松的使用自己的代理类实现替换默认的实现.
框架不包含IoC,也不依赖具体IoC实现,可以使用Asp.Net Core的内置依赖注入或者任何兼容Asp.Net Core的第三方Ioc来继承AspectCore到Asp.NetCore应用中
高性能的异步拦截系统
灵活的配置系统
基于service的而非基于实现类的切面构造
支持扩平台的Asp.Net Core环境
使用AspectCore
从NuGet中安装AspectCore
AspectCore.Extensions.DependencyInjection
package
PM> Install-package AspectCore.Extensions.DependencyInjection
下面我创建了一个Api应用程序.
NuGet安装
AspectCore.Configuration
package
PM> Install-package AspectCore.Configuration
下面我新建了一个拦截器 CustomInterceptorAttribute,继承AbstractInterceptorAttribute(一般情况下继承他即可),他实现IInterceptor接口AspectCore默认实现了基于Attribute
的拦截器配置。
/// <summary>
/// 自定义拦截器
/// </summary>
public class CustomInterceptorAttribute : AbstractInterceptorAttribute
{
/// <summary>
/// 实现抽象方法
/// </summary>
/// <param name="context"></param>
/// <param name="next"></param>
public override async Task Invoke(AspectContext context, AspectDelegate next)
{
try
{
Console.WriteLine("执行之前");
await next(context);//执行被拦截的方法
}
catch (Exception)
{
Console.WriteLine("被拦截的方法出现异常");
throw;
}
finally
{
Console.WriteLine("执行之后");
}
}
}
定义ICustomService
接口和它的实现类CustomService
:
public interface ICustomService
{
DateTime GetDateTime();
}
public class CustomService : ICustomService
{
public DateTime GetDateTime()
{
return DateTime.Now;
}
}
在ValuesController注入ICustomService
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly ICustomService _icustomserveice;
public ValuesController(ICustomService icustomService) {
this._icustomserveice = icustomService;
}
// GET api/values
[HttpGet]
public DateTime Get()
{
return _icustomserveice.GetDateTime();
}
}
注册ICustomService,并创建代理容器
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddTransient<ICustomService,CustomService>();
services.AddMvc();
//全局拦截器。使用AddDynamicProxy(Action<IAspectConfiguration>)的重载方法,其中IAspectConfiguration提供Interceptors注册全局拦截器:
services.ConfigureDynamicProxy(config=> {
config.Interceptors.AddTyped<CustomInterceptorAttribute>();
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
return services.BuildAspectInjectorProvider();
}
作为服务的全局拦截器。在ConfigureServices
中添加:
services.AddTransient<CustomInterceptorAttribute>(provider => new CustomInterceptorAttribute());
作用于特定Service
或Method
的全局拦截器,下面的代码演示了作用于带有Service
后缀的类的全局拦截器:
services.ConfigureDynamicProxy(config =>
{
config.Interceptors.AddTyped<CustomInterceptorAttribute>(method => method.DeclaringType.Name.EndsWith("Service"));
});
通配符拦截器,匹配后缀为Service
services.ConfigureDynamicProxy(config =>
{
config.Interceptors.AddTyped<CustomInterceptorAttribute>(Predicates.ForService("*Service"));
});
在AspectCore中提供NonAspectAttribute
来使得Service
或Method
不被代理:
[NonAspect]
DateTime GetDate();
全局配置忽略条件
services.ConfigureDynamicProxy(config =>
{
//Namespace命名空间下的Service不会被代理
config.NonAspectPredicates.AddNamespace("Namespace");
//最后一级为Namespace的命名空间下的Service不会被代理
config.NonAspectPredicates.AddNamespace("*.Namespace");
//ICustomService接口不会被代理
config.NonAspectPredicates.AddService("ICustomService");
//后缀为Service的接口和类不会被代理
config.NonAspectPredicates.AddService("*Service");
//命名为Method的方法不会被代理
config.NonAspectPredicates.AddMethod("Method");
//后缀为Method的方法不会被代理
config.NonAspectPredicates.AddMethod("*Method");
});
AspectCore: [https://github.com/dotnetcore/AspectCore-Framework]
测试项目地址: [https://github.com/fhcodegit/DotNetAspectCore/tree/master]
dotnetcore实现Aop的更多相关文章
- Asp.Net Core轻量级Aop解决方案:AspectCore
什么是AspectCore Project ? AspectCore Project 是适用于Asp.Net Core 平台的轻量级 Aop(Aspect-oriented programming) ...
- AOP in dotnet :AspectCore的参数拦截支持
距离上一篇AspectCore的介绍发布已经很长一段时间了,这篇文章也早该和大家见面,最近一直忙于适应新工作,并在业余时间有幸向何镇汐,Savorboard,农夫,AlexLEWIS等几位大牛请教学习 ...
- ASP.NET Core 2.1的配置、AOP、缓存、部署、ORM、进程守护、Nginx、Polly【源码】
ps:废话不多说.直接上代码:源码地址:https://github.com/786744873/Asp.Net-Core-2.1-All-Demos/tree/master/src Configur ...
- (6).NET CORE微服务 Micro-Service ---- AOP框架
AOP 框架基础 要求懂的知识:AOP.Filter.反射(Attribute). 如果直接使用 Polly,那么就会造成业务代码中混杂大量的业务无关代码.我们使用 AOP (如果不了解 AOP,请自 ...
- (6)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- AOP框架
AOP 框架基础 要求懂的知识:AOP.Filter.反射(Attribute). 如果直接使用 Polly,那么就会造成业务代码中混杂大量的业务无关代码.我们使用 AOP (如果不了解 AOP,请自 ...
- .net core系列之《对AOP思想的理解及使用AspectCore实现自定义日志拦截》
对于AOP这个名词,相信对于搞过MVC开发的人来说,都很熟悉,里面各种各样的Filter简直是将AOP体现到了极致. 那么什么是AOP呢? AOP(Aspect Oriented Programmin ...
- DispatchProxy实现动态代理及AOP
DispatchProxy类是DotnetCore下的动态代理的类,源码地址:Github,官方文档:MSDN.主要是Activator以及AssemblyBuilder来实现的(请看源码分析),园子 ...
- .NET Core IOC AOP
IOC简介 IOC思想 把类当做组件或服务来看待,组件内一定要高内聚,组件之间一定要低耦合,既然要保持低耦合,那就一定不要轻易的去new什么对象. 那组件之间的交互怎么处理呢?那最好的方式就是把new ...
- .NET 下基于动态代理的 AOP 框架实现揭秘
.NET 下基于动态代理的 AOP 框架实现揭秘 Intro 之前基于 Roslyn 实现了一个简单的条件解析引擎,想了解的可以看这篇文章 https://www.cnblogs.com/weihan ...
随机推荐
- jQuery - 拦截所有Ajax请求(统一处理超时、返回结果、错误状态码 )
样例代码: <html> <head> <title>hangge.com</title> <meta charset="utf-8&q ...
- python科学计算和数据分析常用库
NumPy NumPy最强大的是n维数组,该库还包含基本的线性代数函数.傅立叶变换.随机函数和其他底层语言(如Fortran.C和C++)集成的工具. SciPy SciPy建立在NumPy基础上,它 ...
- September 08th, 2019. Sunday, Week 37th.
A heavy drew refreshed the earth at night. 夜晚厚重的露水滋养着大地. From Leo Tolstoy. Today is the White Drew D ...
- 14. java面向对象 - 基础
一.面向对象主线 1. Java类及类的成员:属性.方法.构造器.代码块.内部类 2. 面向对象三大特征:封装.继承.多态.(抽象性) 3. 其他关键字:this.super.static.final ...
- RTP Payload Format for H264 Video
基础传输结构 rtp中对于h264数据的存储分为两层,分别是 VCL: video coding layer 视频编码层 这是h264中block, macro block 以及 slice级别的定义 ...
- C++ explicit关键字,修饰构造函数,ctor
#include <iostream> // operator Type() 类型操作符重载 // operator int() // operator double() // ... / ...
- VirtualBox中重建Host-Only网卡后无法启动虚拟机
问题: 在删除原有VirtualBox Host-Only虚拟网卡并重新添加后,虚拟机可能会无法启动,出现以下错误 Failed to open/create the internal network ...
- IT兄弟连 HTML5教程 HTML5表单 HTML表单设计2
5 隐藏域 隐藏域不会在表单中显示.如果需要在页面之间传递重要数据,则在<input>标签中设置type属性值为“hidden”建立一个隐藏域.name和value属性是必需的,用来表示 ...
- ETC到底要不要办?有什么好处?
一说到ETC,开车的朋友想必不会陌生.但很多车友却不太愿意办理ETC, 究其原因,主要是一些谣言所致,一传一十传百最后变成了真实的谎言,并且对此深信不疑, 比如下面5个广泛流传的谣言 在来看看 ...
- Web前端基础(17):jQuery基础(四)
1. jQuery的属性操作 jquery的属性操作模块分为四个部分:html属性操作,dom属性操作,类样式操作和值操作 html属性操作:是对html文档中的属性进行读取,设置和移除操作.比如at ...