From bdd32d9d76d20c29bc7d436c0004724d1ca1bbdd Mon Sep 17 00:00:00 2001 From: yubaolee Date: Mon, 31 Mar 2025 09:23:01 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E5=8A=A8=E6=80=81api=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=BC=82=E6=AD=A5=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenAuth.App/DynamicApiApp/DynamicApiApp.cs | 38 +++++++++++++------ OpenAuth.App/Test/TestDynamicApiApp.cs | 8 ++-- .../Controllers/DynamicApiController.cs | 4 +- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/OpenAuth.App/DynamicApiApp/DynamicApiApp.cs b/OpenAuth.App/DynamicApiApp/DynamicApiApp.cs index a60a3db0..ebf9700b 100644 --- a/OpenAuth.App/DynamicApiApp/DynamicApiApp.cs +++ b/OpenAuth.App/DynamicApiApp/DynamicApiApp.cs @@ -7,6 +7,7 @@ using OpenAuth.App.Interface; using OpenAuth.App.Response; using SqlSugar; using OpenAuth.App.Request; +using Microsoft.Extensions.Logging; namespace OpenAuth.App { @@ -20,11 +21,14 @@ namespace OpenAuth.App private readonly IAuth _auth; private readonly IServiceProvider _serviceProvider; - public DynamicApiApp(ISqlSugarClient client, IAuth auth, IServiceProvider serviceProvider) + private readonly ILogger logger; + + public DynamicApiApp(ISqlSugarClient client, IAuth auth, IServiceProvider serviceProvider, ILogger logger) { _client = client; _auth = auth; _serviceProvider = serviceProvider; + this.logger = logger; } /// @@ -345,29 +349,29 @@ namespace OpenAuth.App /// /// 调用参数 /// - public object Invoke(InvokeDynamicReq req) + public async Task Invoke(InvokeDynamicReq request) { var result = new object(); // 获取服务类型 - var serviceType = Type.GetType($"OpenAuth.App.{req.ServiceName}, OpenAuth.App"); + var serviceType = Type.GetType($"OpenAuth.App.{request.ServiceName}, OpenAuth.App"); if (serviceType == null) { - throw new Exception($"未找到服务类型:{req.ServiceName}"); + throw new Exception($"未找到服务类型:{request.ServiceName}"); } // 获取服务实例 var service = _serviceProvider.GetService(serviceType); if (service == null) { - throw new Exception($"无法获取服务实例:{req.ServiceName}"); + throw new Exception($"无法获取服务实例:{request.ServiceName}"); } // 获取方法信息 - var method = serviceType.GetMethod(req.MethodName); + var method = serviceType.GetMethod(request.MethodName); if (method == null) { - throw new Exception($"未找到方法:{req.MethodName}"); + throw new Exception($"未找到方法:{request.MethodName}"); } // 获取方法参数信息 @@ -382,7 +386,7 @@ namespace OpenAuth.App // 尝试从JSON中获取参数值 try { - var json = req.Parameters; + var json = request.Parameters; // 检查参数名是否存在于JSON中 if (json.Contains($"\"{param.Name}\"")) { @@ -414,18 +418,30 @@ namespace OpenAuth.App catch (Exception ex) { // 记录错误日志 - Console.WriteLine($"反序列化参数 {param.Name} 失败: {ex.Message}"); + logger.LogError($"反序列化参数 {param.Name} 失败: {ex.Message}"); // 反序列化失败,使用默认值 paramValues[i] = param.HasDefaultValue ? param.DefaultValue : null; } } - // 使用参数数组调用方法 + // 调用方法并处理返回值 result = method.Invoke(service, paramValues); + + // 如果返回值是Task + if (result is Task task) + { + await task; // 异步等待任务完成 + + // 获取Task的实际结果 + var resultProperty = task.GetType().GetProperty("Result"); + if (resultProperty != null) + { + return resultProperty.GetValue(task); + } + } return result; } } - } \ No newline at end of file diff --git a/OpenAuth.App/Test/TestDynamicApiApp.cs b/OpenAuth.App/Test/TestDynamicApiApp.cs index ae7e3f32..ff4dd7e8 100644 --- a/OpenAuth.App/Test/TestDynamicApiApp.cs +++ b/OpenAuth.App/Test/TestDynamicApiApp.cs @@ -56,11 +56,11 @@ namespace OpenAuth.App.Test } [Test] - public void TestInvoke() + public async Task TestInvoke() { var app = _autofacServiceProvider.GetService(); - var obj = app.Invoke(new InvokeDynamicReq { ServiceName = "UserManagerApp", MethodName = "GetParent", + var obj = await app.Invoke(new InvokeDynamicReq { ServiceName = "UserManagerApp", MethodName = "GetParent", Parameters = "{\"userid\":\"0ceff0f8-f848-440c-bc26-d8605ac858cd\"}" }); Console.WriteLine(JsonHelper.Instance.Serialize(obj)); } @@ -70,12 +70,12 @@ namespace OpenAuth.App.Test { var app = _autofacServiceProvider.GetService(); - var obj = app.Invoke(new InvokeDynamicReq { + var obj = await app.Invoke(new InvokeDynamicReq { ServiceName = "UserManagerApp", MethodName = "Load", Parameters = "{\"request\":{\"page\":1,\"limit\":10,\"key\":\"dddd\"}}" }); - Console.WriteLine(obj.ToString()); + Console.WriteLine(JsonHelper.Instance.Serialize(obj)); } } diff --git a/OpenAuth.WebApi/Controllers/DynamicApiController.cs b/OpenAuth.WebApi/Controllers/DynamicApiController.cs index c0fcbe81..7d89a204 100644 --- a/OpenAuth.WebApi/Controllers/DynamicApiController.cs +++ b/OpenAuth.WebApi/Controllers/DynamicApiController.cs @@ -155,12 +155,12 @@ namespace OpenAuth.WebApi.Controllers /// [HttpPost] [AllowAnonymous] - public Response Invoke([FromBody] InvokeDynamicReq req) + public async Task> Invoke([FromBody] InvokeDynamicReq req) { var result = new Response(); try { - result.Result = _app.Invoke(req); + result.Result = await _app.Invoke(req); } catch (Exception ex) {