Asp.NetCore實作Aop,AspectCore實作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
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]