@@ -12,72 +12,46 @@ namespace DotNetCoreRpc.Client
1212{
1313 public class HttpRequestInterceptor : IInterceptor
1414 {
15- private readonly HttpClient _httpClient ;
15+ private readonly RequestHandler _requestHandler ;
1616 public HttpRequestInterceptor ( HttpClient httpClient )
1717 {
18- _httpClient = httpClient ;
18+ _requestHandler = new RequestHandler ( httpClient ) ;
1919 }
2020
2121 public void Intercept ( IInvocation invocation )
2222 {
23- HandleRequest ( invocation ) . GetAwaiter ( ) . GetResult ( ) ;
24- }
23+ var methodReturnType = invocation . Method . ReturnType . GetTypeInfo ( ) ;
2524
26- private async Task HandleRequest ( IInvocation invocation )
27- {
28- var methodInfo = invocation . Method ;
29- var requestModel = new RequestModel
30- {
31- TypeFullName = methodInfo . DeclaringType . FullName ,
32- MethodName = methodInfo . Name ,
33- Paramters = invocation . Arguments
34- } ;
35- HttpContent httpContent = new StringContent ( requestModel . ToJson ( ) ) ;
36- httpContent . Headers . ContentType = new MediaTypeHeaderValue ( "application/json" ) ;
37- var responseMessage = await _httpClient . PostAsync ( "/DotNetCoreRpc/ServerRequest" , httpContent ) ;
38- if ( responseMessage . StatusCode == HttpStatusCode . OK )
25+ if ( ! methodReturnType . IsAsync ( ) )
3926 {
40- var methodReturnType = methodInfo . ReturnType . GetTypeInfo ( ) ;
41- byte [ ] result = await responseMessage . Content . ReadAsByteArrayAsync ( ) ;
42- if ( result != null && result . Length != 0 )
27+ var result = _requestHandler . SyncResultHandle ( invocation . Method , invocation . Arguments ) ;
28+ if ( result == null )
4329 {
44- ResponseModel responseModel = result . FromJson < ResponseModel > ( ) ;
45- if ( responseModel . Code != ( int ) HttpStatusCode . OK )
46- {
47- throw new Exception ( $ "请求出错,返回内容:{ Encoding . UTF8 . GetString ( result ) } ") ;
48- }
49- if ( responseModel . Data != null )
50- {
51- if ( ! methodReturnType . IsAsync ( ) )
52- {
53- invocation . ReturnValue = responseModel . Data . ToJson ( ) . FromJson ( methodInfo . ReturnType ) ;
54- return ;
55- }
30+ return ;
31+ }
5632
57- var returnValue = responseModel . Data . ToJson ( ) . FromJson ( methodReturnType . GetGenericArguments ( ) [ 0 ] ) ;
58- var resultType = invocation . Method . ReturnType . GetGenericArguments ( ) [ 0 ] ;
59- if ( methodReturnType . IsTaskWithResult ( ) )
60- {
61- invocation . ReturnValue = TaskUtils . TaskResultFunc ( resultType ) . Invoke ( returnValue ) ;
62- return ;
63- }
33+ invocation . ReturnValue = result ;
34+ return ;
35+ }
6436
65- if ( methodReturnType . IsValueTaskWithResult ( ) )
66- {
67- invocation . ReturnValue = TaskUtils . ValueTaskResultFunc ( resultType ) . Invoke ( returnValue ) ;
68- return ;
69- }
70- }
37+ if ( methodReturnType . IsTask ( ) || methodReturnType . IsValueTask ( ) )
38+ {
39+ invocation . ReturnValue = Task . CompletedTask ;
40+ return ;
41+ }
7142
72- if ( methodReturnType . IsTask ( ) || methodReturnType . IsValueTask ( ) )
73- {
74- invocation . ReturnValue = Task . CompletedTask ;
75- return ;
76- }
77- }
43+ if ( methodReturnType . IsTaskWithResult ( ) )
44+ {
45+ invocation . ReturnValue = _requestHandler . GetTaskResultHandleFunc ( methodReturnType ) . Invoke ( invocation . Method , invocation . Arguments ) ;
7846 return ;
7947 }
80- throw new Exception ( $ "请求异常,StatusCode:{ responseMessage . StatusCode } ") ;
81- }
48+
49+ if ( methodReturnType . IsValueTaskWithResult ( ) )
50+ {
51+ invocation . ReturnValue = _requestHandler . GetValueResultHandleFunc ( methodReturnType ) . Invoke ( invocation . Method , invocation . Arguments ) ;
52+ return ;
53+ }
54+ }
55+
8256 }
8357}
0 commit comments