Browse Source

Cache CallRequestAsyncMethod.

pull/10040/head
maliming 5 years ago
parent
commit
b65a8d7dc5
  1. 36
      framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs

36
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs

@ -16,6 +16,17 @@ namespace Volo.Abp.Http.Client.DynamicProxying
{ {
public class DynamicHttpProxyInterceptor<TService> : AbpInterceptor, ITransientDependency public class DynamicHttpProxyInterceptor<TService> : AbpInterceptor, ITransientDependency
{ {
// ReSharper disable once StaticMemberInGenericType
protected static MethodInfo CallRequestAsyncMethod { get; }
static DynamicHttpProxyInterceptor()
{
CallRequestAsyncMethod = typeof(DynamicHttpProxyInterceptor<TService>)
.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
.First(m => m.Name == nameof(CallRequestAsync) && m.IsGenericMethodDefinition);
}
public ILogger<DynamicHttpProxyInterceptor<TService>> Logger { get; set; } public ILogger<DynamicHttpProxyInterceptor<TService>> Logger { get; set; }
protected DynamicHttpProxyInterceptorClientProxy<TService> InterceptorClientProxy { get; } protected DynamicHttpProxyInterceptorClientProxy<TService> InterceptorClientProxy { get; }
protected AbpHttpClientOptions ClientOptions { get; } protected AbpHttpClientOptions ClientOptions { get; }
@ -52,25 +63,19 @@ namespace Volo.Abp.Http.Client.DynamicProxying
} }
else else
{ {
var result = (Task)InterceptorClientProxy var returnType = invocation.Method.ReturnType.GenericTypeArguments[0];
.GetType() var result = (Task)CallRequestAsyncMethod
.GetMethods() .MakeGenericMethod(returnType)
.Single(m => m.Name == nameof(InterceptorClientProxy.CallRequestAsync) && .Invoke(this, new object[] { context });
m.IsGenericMethod &&
m.GetParameters().Any(x => x.ParameterType == typeof(ClientProxyRequestContext)))
.MakeGenericMethod(invocation.Method.ReturnType.GenericTypeArguments[0])
.Invoke(InterceptorClientProxy, new object[] { context });
invocation.ReturnValue = await GetResultAsync( invocation.ReturnValue = await GetResultAsync(result, returnType);
result,
invocation.Method.ReturnType.GetGenericArguments()[0]
);
} }
} }
protected virtual async Task<ActionApiDescriptionModel> GetActionApiDescriptionModel(IAbpMethodInvocation invocation) protected virtual async Task<ActionApiDescriptionModel> GetActionApiDescriptionModel(IAbpMethodInvocation invocation)
{ {
var clientConfig = ClientOptions.HttpClientProxies.GetOrDefault(typeof(TService)) ?? throw new AbpException($"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}."); var clientConfig = ClientOptions.HttpClientProxies.GetOrDefault(typeof(TService)) ??
throw new AbpException($"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}.");
var remoteServiceConfig = await RemoteServiceConfigurationProvider.GetConfigurationOrDefaultAsync(clientConfig.RemoteServiceName); var remoteServiceConfig = await RemoteServiceConfigurationProvider.GetConfigurationOrDefaultAsync(clientConfig.RemoteServiceName);
var client = HttpClientFactory.Create(clientConfig.RemoteServiceName); var client = HttpClientFactory.Create(clientConfig.RemoteServiceName);
@ -82,6 +87,11 @@ namespace Volo.Abp.Http.Client.DynamicProxying
); );
} }
protected virtual async Task<T> CallRequestAsync<T>(ClientProxyRequestContext context)
{
return await InterceptorClientProxy.CallRequestAsync<T>(context);
}
protected virtual async Task<object> GetResultAsync(Task task, Type resultType) protected virtual async Task<object> GetResultAsync(Task task, Type resultType)
{ {
await task; await task;

Loading…
Cancel
Save