diff --git a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs index a9941384a2..ddf3528aca 100644 --- a/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs +++ b/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs @@ -31,7 +31,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying { GenericInterceptAsyncMethod = typeof(DynamicHttpProxyInterceptor) .GetMethods(BindingFlags.NonPublic | BindingFlags.Instance) - .First(m => m.Name == nameof(InterceptAsync) && m.IsGenericMethodDefinition); + .First(m => m.Name == nameof(MakeRequestAndGetResultAsync) && m.IsGenericMethodDefinition); } public DynamicHttpProxyInterceptor( @@ -48,9 +48,17 @@ namespace Volo.Abp.Http.Client.DynamicProxying public override void Intercept(IAbpMethodInvocation invocation) { - //TODO: Handle this differently because InterceptAsync assumes that given method is async! - - AsyncHelper.RunSync(() => InterceptAsync(invocation)); + if (invocation.Method.ReturnType == typeof(void)) + { + AsyncHelper.RunSync(() => MakeRequest(invocation)); + } + else + { + invocation.ReturnValue = _jsonSerializer.Deserialize( + invocation.Method.ReturnType, + AsyncHelper.RunSync(() => MakeRequest(invocation)) + ); + } } public override Task InterceptAsync(IAbpMethodInvocation invocation) @@ -67,19 +75,9 @@ namespace Volo.Abp.Http.Client.DynamicProxying return Task.CompletedTask; } - private async Task InterceptAsync(IAbpMethodInvocation invocation) + private async Task MakeRequestAndGetResultAsync(IAbpMethodInvocation invocation) { - var content = await MakeRequest(invocation); - - var result = JsonConvert.DeserializeObject( - content, - typeof(T), - new JsonSerializerSettings - { - ContractResolver = new CamelCasePropertyNamesContractResolver() - }); - - return (T)result; + return _jsonSerializer.Deserialize(await MakeRequest(invocation)); } private async Task MakeRequest(IAbpMethodInvocation invocation) diff --git a/src/Volo.Abp/Volo/Abp/Json/IJsonSerializer.cs b/src/Volo.Abp/Volo/Abp/Json/IJsonSerializer.cs index 481e191348..6b1a4f4de4 100644 --- a/src/Volo.Abp/Volo/Abp/Json/IJsonSerializer.cs +++ b/src/Volo.Abp/Volo/Abp/Json/IJsonSerializer.cs @@ -1,9 +1,13 @@ -namespace Volo.Abp.Json +using System; + +namespace Volo.Abp.Json { public interface IJsonSerializer { string Serialize(object obj, bool camelCase = true, bool indented = false); - T Deserialize(string jsonString); + T Deserialize(string jsonString, bool camelCase = true); + + object Deserialize(Type type, string jsonString, bool camelCase = true); } } \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs b/src/Volo.Abp/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs index f6c746f47a..2d4b403a0e 100644 --- a/src/Volo.Abp/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs +++ b/src/Volo.Abp/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs @@ -1,3 +1,4 @@ +using System; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Volo.Abp.DependencyInjection; @@ -13,33 +14,38 @@ namespace Volo.Abp.Json.Newtonsoft _dateTimeConverter = dateTimeConverter; } - public string Serialize(object obj, bool camelCase = false, bool indented = false) + public string Serialize(object obj, bool camelCase = true, bool indented = false) { - var serializerSettings = CreateDefaultSerializerSettings(); + return JsonConvert.SerializeObject(obj, CreateSerializerSettings(camelCase, indented)); + } + + public T Deserialize(string jsonString, bool camelCase = true) + { + return JsonConvert.DeserializeObject(jsonString, CreateSerializerSettings(camelCase)); + } + + public object Deserialize(Type type, string jsonString, bool camelCase = true) + { + return JsonConvert.DeserializeObject(jsonString, type, CreateSerializerSettings(camelCase)); + } + + protected virtual JsonSerializerSettings CreateSerializerSettings(bool camelCase = true, bool indented = false) + { + var settings = new JsonSerializerSettings(); + + settings.Converters.Insert(0, _dateTimeConverter); if (camelCase) { - serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); + settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); } if (indented) { - serializerSettings.Formatting = Formatting.Indented; + settings.Formatting = Formatting.Indented; } - - return JsonConvert.SerializeObject(obj, serializerSettings); - } - - protected virtual JsonSerializerSettings CreateDefaultSerializerSettings() - { - var settings = new JsonSerializerSettings(); - settings.Converters.Insert(0, _dateTimeConverter); + return settings; } - - public T Deserialize(string jsonString) - { - return JsonConvert.DeserializeObject(jsonString); - } } } \ No newline at end of file