Skip to content

Commit

Permalink
接口只有一个基础类型入参时,客户端可能用基础类型封包传递(字符串)。
Browse files Browse the repository at this point in the history
例如接口 Say(String text),客户端可用 InvokeAsync<Object>("Say", "Hello NewLife!")
  • Loading branch information
nnhy committed Nov 11, 2023
1 parent f58dd48 commit 93084e2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions NewLife.Remoting/IApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object>("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);
Expand Down
32 changes: 32 additions & 0 deletions XUnitTest/ApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleController>();
server.Start();

using var client = new ApiClient($"tcp://127.0.0.1:{server.Port}");

var rs = await client.InvokeAsync<String>("Simple/Say", "Hello NewLife!");
Assert.Equal("Say: Hello NewLife!", rs);

var time = DateTime.Now;
rs = await client.InvokeAsync<String>("Simple/Login", time);
Assert.Equal($"Login: {time.ToFullString()}", rs);

rs = await client.InvokeAsync<String>("Simple/Login", time.ToFullString());
Assert.Equal($"Login: {time.ToFullString()}", rs);

rs = await client.InvokeAsync<String>("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()}";
}
}

0 comments on commit 93084e2

Please sign in to comment.