From ee42b9d7dd0567d4236e791f07ed593f93a77619 Mon Sep 17 00:00:00 2001 From: Monk Date: Tue, 23 Mar 2021 11:09:25 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B1=E2=80=8D=E2=99=82=EF=B8=8F=20adjus?= =?UTF-8?q?t=20remote=20request=20interceptor=20code.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Attributes/InterceptorAttribute.cs | 3 +- .../Proxies/HttpDispatchProxy.cs | 64 +++++++++---------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/framework/Furion/RemoteRequest/Attributes/InterceptorAttribute.cs b/framework/Furion/RemoteRequest/Attributes/InterceptorAttribute.cs index 248199d1b44..5679c196e5b 100644 --- a/framework/Furion/RemoteRequest/Attributes/InterceptorAttribute.cs +++ b/framework/Furion/RemoteRequest/Attributes/InterceptorAttribute.cs @@ -6,7 +6,8 @@ namespace Furion.RemoteRequest /// /// 远程请求参数拦截器 /// - [SkipScan, AttributeUsage(AttributeTargets.Parameter)] + /// 如果贴在静态方法中,则为全局拦截 + [SkipScan, AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)] public class InterceptorAttribute : Attribute { /// diff --git a/framework/Furion/RemoteRequest/Proxies/HttpDispatchProxy.cs b/framework/Furion/RemoteRequest/Proxies/HttpDispatchProxy.cs index 0b9697c9cda..e490553e266 100644 --- a/framework/Furion/RemoteRequest/Proxies/HttpDispatchProxy.cs +++ b/framework/Furion/RemoteRequest/Proxies/HttpDispatchProxy.cs @@ -14,7 +14,7 @@ namespace Furion.RemoteRequest { /// - /// 远程请求实现类 + /// 远程请求实现类(以下代码还需进一步优化性能,启动时把所有扫描缓存起来) /// [SkipScan] public class HttpDispatchProxy : AspectDispatchProxy, IDispatchProxy @@ -35,10 +35,7 @@ public class HttpDispatchProxy : AspectDispatchProxy, IDispatchProxy /// /// /// - public override object Invoke(MethodInfo method, object[] args) - { - throw new NotSupportedException("Please use asynchronous operation mode."); - } + public override object Invoke(MethodInfo method, object[] args) => throw new NotSupportedException("Please use asynchronous operation mode."); /// /// 拦截异步无返回方法 @@ -232,36 +229,39 @@ private static void SetJsonSerialization(MethodInfo method, IEnumerable private static void CallGlobalInterceptors(HttpClientPart httpClientPart, Type declareType) { - // 加载请求拦截 - var onRequestingMethod = declareType.GetMethod(nameof(httpClientPart.OnRequesting)); - if (onRequestingMethod != null) - { - var onRequesting = (Action)Delegate.CreateDelegate(typeof(Action), onRequestingMethod); - httpClientPart.OnRequesting(onRequesting); - } - - // 加载响应拦截 - var OnResponsingMethod = declareType.GetMethod(nameof(httpClientPart.OnResponsing)); - if (OnResponsingMethod != null) - { - var onResponsing = (Action)Delegate.CreateDelegate(typeof(Action), OnResponsingMethod); - httpClientPart.OnResponsing(onResponsing); - } + // 获取所有静态方法且贴有 [Interceptor] 特性 + var interceptorMethods = declareType.GetMethods(BindingFlags.Static) + .Where(u => u.IsDefined(typeof(InterceptorAttribute), true)); - // 加载 Client 配置拦截 - var onClientCreatingMethod = declareType.GetMethod(nameof(httpClientPart.OnClientCreating)); - if (onClientCreatingMethod != null) + foreach (var method in interceptorMethods) { - var onClientCreating = (Action)Delegate.CreateDelegate(typeof(Action), onClientCreatingMethod); - httpClientPart.OnClientCreating(onClientCreating); - } + // 获取拦截器类型 + var interceptor = method.GetCustomAttribute(); + switch (interceptor.Type) + { + // 加载请求拦截 + case InterceptorTypes.Request: + var onRequesting = (Action)Delegate.CreateDelegate(typeof(Action), method); + httpClientPart.OnRequesting(onRequesting); + break; + // 加载响应拦截 + case InterceptorTypes.Response: + var onResponsing = (Action)Delegate.CreateDelegate(typeof(Action), method); + httpClientPart.OnResponsing(onResponsing); + break; + // 加载 Client 配置拦截 + case InterceptorTypes.HttpClient: + var onClientCreating = (Action)Delegate.CreateDelegate(typeof(Action), method); + httpClientPart.OnClientCreating(onClientCreating); + break; + // 加载异常拦截 + case InterceptorTypes.Exception: + var onException = (Action)Delegate.CreateDelegate(typeof(Action), method); + httpClientPart.OnException(onException); + break; - // 加载异常拦截 - var onExceptionMethod = declareType.GetMethod(nameof(httpClientPart.OnException)); - if (onExceptionMethod != null) - { - var onException = (Action)Delegate.CreateDelegate(typeof(Action), onExceptionMethod); - httpClientPart.OnException(onException); + default: break; + } } }