Skip to content

Commit

Permalink
👱‍♂️ adjust remote request interceptor code.
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkSoul committed Mar 23, 2021
1 parent 69b9def commit ee42b9d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace Furion.RemoteRequest
/// <summary>
/// 远程请求参数拦截器
/// </summary>
[SkipScan, AttributeUsage(AttributeTargets.Parameter)]
/// <remarks>如果贴在静态方法中,则为全局拦截</remarks>
[SkipScan, AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Method)]
public class InterceptorAttribute : Attribute
{
/// <summary>
Expand Down
64 changes: 32 additions & 32 deletions framework/Furion/RemoteRequest/Proxies/HttpDispatchProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Furion.RemoteRequest
{
/// <summary>
/// 远程请求实现类
/// 远程请求实现类(以下代码还需进一步优化性能,启动时把所有扫描缓存起来)
/// </summary>
[SkipScan]
public class HttpDispatchProxy : AspectDispatchProxy, IDispatchProxy
Expand All @@ -35,10 +35,7 @@ public class HttpDispatchProxy : AspectDispatchProxy, IDispatchProxy
/// <param name="method"></param>
/// <param name="args"></param>
/// <returns></returns>
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.");

/// <summary>
/// 拦截异步无返回方法
Expand Down Expand Up @@ -232,36 +229,39 @@ private static void SetJsonSerialization(MethodInfo method, IEnumerable<MethodPa
/// <param name="declareType"></param>
private static void CallGlobalInterceptors(HttpClientPart httpClientPart, Type declareType)
{
// 加载请求拦截
var onRequestingMethod = declareType.GetMethod(nameof(httpClientPart.OnRequesting));
if (onRequestingMethod != null)
{
var onRequesting = (Action<HttpRequestMessage>)Delegate.CreateDelegate(typeof(Action<HttpRequestMessage>), onRequestingMethod);
httpClientPart.OnRequesting(onRequesting);
}

// 加载响应拦截
var OnResponsingMethod = declareType.GetMethod(nameof(httpClientPart.OnResponsing));
if (OnResponsingMethod != null)
{
var onResponsing = (Action<HttpResponseMessage>)Delegate.CreateDelegate(typeof(Action<HttpResponseMessage>), 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<HttpClient>)Delegate.CreateDelegate(typeof(Action<HttpClient>), onClientCreatingMethod);
httpClientPart.OnClientCreating(onClientCreating);
}
// 获取拦截器类型
var interceptor = method.GetCustomAttribute<InterceptorAttribute>();
switch (interceptor.Type)
{
// 加载请求拦截
case InterceptorTypes.Request:
var onRequesting = (Action<HttpRequestMessage>)Delegate.CreateDelegate(typeof(Action<HttpRequestMessage>), method);
httpClientPart.OnRequesting(onRequesting);
break;
// 加载响应拦截
case InterceptorTypes.Response:
var onResponsing = (Action<HttpResponseMessage>)Delegate.CreateDelegate(typeof(Action<HttpResponseMessage>), method);
httpClientPart.OnResponsing(onResponsing);
break;
// 加载 Client 配置拦截
case InterceptorTypes.HttpClient:
var onClientCreating = (Action<HttpClient>)Delegate.CreateDelegate(typeof(Action<HttpClient>), method);
httpClientPart.OnClientCreating(onClientCreating);
break;
// 加载异常拦截
case InterceptorTypes.Exception:
var onException = (Action<HttpResponseMessage, string>)Delegate.CreateDelegate(typeof(Action<HttpResponseMessage, string>), method);
httpClientPart.OnException(onException);
break;

// 加载异常拦截
var onExceptionMethod = declareType.GetMethod(nameof(httpClientPart.OnException));
if (onExceptionMethod != null)
{
var onException = (Action<HttpResponseMessage, string>)Delegate.CreateDelegate(typeof(Action<HttpResponseMessage, string>), onExceptionMethod);
httpClientPart.OnException(onException);
default: break;
}
}
}

Expand Down

0 comments on commit ee42b9d

Please sign in to comment.