From 93084e221eda292057fcaa2c1a7004cb2fc6d71b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sat, 11 Nov 2023 14:24:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=8F=AA=E6=9C=89=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=9F=BA=E7=A1=80=E7=B1=BB=E5=9E=8B=E5=85=A5=E5=8F=82?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E7=94=A8=E5=9F=BA=E7=A1=80=E7=B1=BB=E5=9E=8B=E5=B0=81=E5=8C=85?= =?UTF-8?q?=E4=BC=A0=E9=80=92=EF=BC=88=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=89?= =?UTF-8?q?=E3=80=82=20=E4=BE=8B=E5=A6=82=E6=8E=A5=E5=8F=A3=20Say(String?= =?UTF-8?q?=20text)=EF=BC=8C=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=8F=AF=E7=94=A8?= =?UTF-8?q?=20InvokeAsync("Say",=20"Hello=20NewLife!")?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Remoting/IApiHandler.cs | 5 +++-- XUnitTest/ApiTest.cs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/NewLife.Remoting/IApiHandler.cs b/NewLife.Remoting/IApiHandler.cs index 341e20d..8566016 100644 --- a/NewLife.Remoting/IApiHandler.cs +++ b/NewLife.Remoting/IApiHandler.cs @@ -198,8 +198,9 @@ public virtual ControllerContext Prepare(IApiSession session, String action, Pac var pis = method.GetParameters(); if (pis == null || pis.Length <= 0) return ps; - // 接口只有一个入参时,客户端可能用基础类型封包传递 - if (pis.Length == 1 && dic == null && args != null) + // 接口只有一个基础类型入参时,客户端可能用基础类型封包传递(字符串)。 + // 例如接口 Say(String text),客户端可用 InvokeAsync("Say", "Hello NewLife!") + if (pis.Length == 1 && pis[0].ParameterType.GetTypeCode() != TypeCode.Object && dic.Count == 0 && args != null) { var pi = pis[0]; ps[pi.Name] = args.ToStr().ChangeType(pi.ParameterType); diff --git a/XUnitTest/ApiTest.cs b/XUnitTest/ApiTest.cs index 70fd209..2c938de 100644 --- a/XUnitTest/ApiTest.cs +++ b/XUnitTest/ApiTest.cs @@ -219,4 +219,36 @@ class SPService public Int64 Test(String key) => _cache.Increment(key, 123); } + + [Fact] + public async void SimpleType() + { + using var server = new ApiServer(12377); + server.Log = XTrace.Log; + server.EncoderLog = XTrace.Log; + server.Register(); + server.Start(); + + using var client = new ApiClient($"tcp://127.0.0.1:{server.Port}"); + + var rs = await client.InvokeAsync("Simple/Say", "Hello NewLife!"); + Assert.Equal("Say: Hello NewLife!", rs); + + var time = DateTime.Now; + rs = await client.InvokeAsync("Simple/Login", time); + Assert.Equal($"Login: {time.ToFullString()}", rs); + + rs = await client.InvokeAsync("Simple/Login", time.ToFullString()); + Assert.Equal($"Login: {time.ToFullString()}", rs); + + rs = await client.InvokeAsync("Simple/Login", time.ToString()); + Assert.Equal($"Login: {time.ToFullString()}", rs); + } + + class SimpleController + { + public String Say(String text) => $"Say: {text}"; + + public String Login(DateTime time) => $"Login: {time.ToFullString()}"; + } } \ No newline at end of file