日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

聊聊AspectCore动态代理中的拦截器(一)

發(fā)布時間:2023/12/4 67 豆豆
生活随笔 收集整理的這篇文章主要介紹了 聊聊AspectCore动态代理中的拦截器(一) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

在上一篇文章使用AspectCore動態(tài)代理中,簡單說明了AspectCore.DynamicProxy的使用方式,由于介紹的比較淺顯,也有不少同學(xué)留言詢問攔截器的配置,那么在這篇文章中,我們來詳細(xì)看一下AspectCore中的攔截器使用。

兩種配置方式

在AspectCore中,提供攔截器的特性配置和全局配置兩種使用方式,并且分別提供AbstractInterceptor(可用于全局?jǐn)r截器配置)和AbstractInterceptorAttribute(可同時用于全局配置和特性配置)兩個攔截器基類。下面來分別演示兩個攔截器配置方式的使用:

  • 特性攔截器。我們繼承AbstractInterceptorAttribute來實(shí)現(xiàn)一個自己的特性攔截器

public class CustomInterceptorAttribute : AbstractInterceptorAttribute{ ? ?
? ? ?public override Task Invoke(AspectContext context, AspectDelegate next) ? ? { ? ? ?
? ? ? ?? return context.Invoke(next);}}

那么此時CustomInterceptorAttribute可以標(biāo)記在需要攔截的接口,類或者方法上來開啟攔截。

  • 全局?jǐn)r截器配置。我們繼承AbstractInterceptor來實(shí)現(xiàn)一個自己的特性攔截器(除不能作為Attribute標(biāo)記在口,類或者方法上之外,AbstractInterceptor和AbstractInterceptorAttribute并無任何區(qū)別)

public class CustomInterceptor : AbstractInterceptor{ ? ?
? ? ? public override Task Invoke(AspectContext context, AspectDelegate next) ? ?? { ? ?
? ? ? ?? ? return context.Invoke(next);}}

現(xiàn)在我們已經(jīng)定義了我們自己的攔截器,我使用Microsoft.Extensions.DependencyInjection的集成方式來演示全局?jǐn)r截器的配置(需安裝AspectCore.Extensions.DependencyInjection包):

IServiceCollection services = new ServiceCollection(); services.AddDynamicProxy(config => {config.Interceptors.AddTyped<CustomInterceptor>(); }); IServiceProvider serviceProvider = services.BuildAspectCoreServiceProvider();

CustomInterceptor便可以攔截由serviceProvider創(chuàng)建的任何服務(wù)的方法。

三種攔截器類型

在AspectCore中,提供了TypedInterceptor,ServiceInterceptor,DelegateInterceptor三種攔截器的激活類型。

  • TypedInterceptor
    標(biāo)記在接口,類或者方法上的特性攔截器或者使用上面config.Interceptors.AddTyped<CustomInterceptor>();配置的全局?jǐn)r截器,這類攔截器對于每個方法具有唯一的實(shí)例。

  • ServiceInterceptor
    注冊到DI并從DI激活使用的攔截器。這類攔截器的生命周期同注冊到DI時的生命周期一致。如下面我們注冊一個瞬態(tài)的ServiceInterceptor:

IServiceCollection services = new ServiceCollection();services.AddTransient<CustomInterceptor>();

我們可以使用ServiceInterceptor特性激活注冊到DI中的攔截器:

[ServiceInterceptor(typeof(CustomInterceptor))]public interface IService{ ? ?void Foo(); }

或者使用全局配置:

IServiceCollection services = new ServiceCollection();services.AddTransient<CustomInterceptor>();services.AddDynamicProxy(config =>{config.Interceptors.AddServiced<CustomInterceptor>();});IServiceProvider serviceProvider = services.BuildAspectCoreServiceProvider();
  • DelegateInterceptor
    在使用全局的攔截器配置時,我們也可以不定義具體的攔截器類,而直接使用簽名為Func<AspectDelegate, AspectDelegate>或Func<AspectContext, AspectDelegate, Task>的委托來執(zhí)行攔截,如下面:

IServiceCollection services = new ServiceCollection();services.AddTransient<CustomInterceptor>();services.AddDynamicProxy(config => {config.Interceptors.AddDelegate( async (content, next) =>{Console.WriteLine("delegate interceptor"); ? ? ? ?await content.Invoke(next);}); }); IServiceProvider serviceProvider = services.BuildAspectCoreServiceProvider();

使用通配符或者委托配置攔截器

在AspectCore中配置全局?jǐn)r截器時,可以使用通配符或者委托來限定攔截器的作用范圍。
內(nèi)置提供了Predicates.ForMethod,Predicates.ForService,Predicates.ForNameSpace三個通配符函數(shù):

services.AddDynamicProxy(config =>{config.Interceptors.AddTyped<CustomInterceptor>(Predicates.ForMethod("*Query")); //攔截所有Query后綴的方法config.Interceptors.AddTyped<CustomInterceptor>(Predicates.ForService("*Repository")); //攔截所有Repository后綴的類或接口config.Interceptors.AddTyped<CustomInterceptor>(Predicates.ForNamespace("AspectCoreDemo.*")); //攔截所有AspectCoreDemo及其子命名空間下面的接口或類});

有問題反饋

如果您有任何問題,請?zhí)峤?Issue?給我們。
Github :?https://github.com/dotnetcore/AspectCore-Framework
AspectCore QQ群: 306531723

原文:http://www.cnblogs.com/liuhaoyang/p/interceptor-in-aspectcore-part-1.html


.NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com

總結(jié)

以上是生活随笔為你收集整理的聊聊AspectCore动态代理中的拦截器(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。