Skip to content

Commit 6e84397

Browse files
committed
优化参数注入逻辑并升级依赖包版本
升级 NewLife 相关依赖至 2026.714/2026.708,提升兼容性。优化 ApiClient、WsClient、AliyunClient 参数注入,避免数组/列表强转字典,减少重复转换,提升性能和类型兼容性。AliyunClient 请求签名处理更严谨,统一类型判断方式。
1 parent 01a41f4 commit 6e84397

11 files changed

Lines changed: 81 additions & 38 deletions

File tree

Benchmark/NewLife.Remoting.Benchmarks.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
16-
<PackageReference Include="NewLife.Core" Version="11.17.2026.706-beta0929" />
16+
<PackageReference Include="NewLife.Core" Version="11.17.2026.714-beta0817" />
1717
</ItemGroup>
1818

1919
<ItemGroup>

NewLife.Remoting/ApiClient.cs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using NewLife.Remoting.Http;
99
using NewLife.Serialization;
1010
using NewLife.Threading;
11+
using System.Collections;
1112
using System.Collections.Concurrent;
1213
using System.Runtime.CompilerServices;
1314
#if !NET40
@@ -308,31 +309,28 @@ public virtual Int32 InvokeOneWay(String action, Object? args = null, Byte flag
308309

309310
LastActive = DateTime.Now;
310311

311-
// 令牌
312-
if (!Token.IsNullOrEmpty() && args != null)
313-
{
314-
var dic = args.ToDictionary();
315-
if (!dic.ContainsKey("Token")) dic["Token"] = Token;
316-
args = dic;
317-
}
318-
319312
// 自动注入当前 Span 上下文字符串到 Headers,包含 TraceId+SpanId 便于星尘构建调用链上下级关系
320313
var currentSpan = DefaultSpan.Current?.ToString();
321314
if (!currentSpan.IsNullOrEmpty() && !Headers.ContainsKey("TraceId"))
322315
Headers["TraceId"] = currentSpan;
323316

324-
// Headers 注入
325-
if (Headers.Count > 0 && args != null)
317+
// 令牌与请求头一并注入参数集合。一次类型检查、一次 ToDictionary,避免重复转换
318+
if (args != null && (!Token.IsNullOrEmpty() || Headers.Count > 0))
326319
{
327320
var type = args.GetType();
328321
// 数组/列表类型不转换为字典,避免丢失原始数据
329-
if (!type.IsArray && !typeof(System.Collections.IList).IsAssignableFrom(type))
322+
if (!type.IsArray && !typeof(IList).IsAssignableFrom(type))
330323
{
331324
var dic = args.ToDictionary();
325+
326+
if (!Token.IsNullOrEmpty() && !dic.ContainsKey("Token"))
327+
dic["Token"] = Token;
328+
332329
foreach (var item in Headers)
333330
{
334331
if (!dic.ContainsKey(item.Key)) dic[item.Key] = item.Value;
335332
}
333+
336334
args = dic;
337335
}
338336
}
@@ -465,12 +463,30 @@ public Int32 InvokeWithClient(ISocketClient client, String action, Object? args,
465463

466464
LastActive = DateTime.Now;
467465

468-
// 令牌
469-
if (!Token.IsNullOrEmpty() && args != null)
466+
// 自动注入当前 Span 上下文字符串到 Headers,包含 TraceId+SpanId 便于星尘构建调用链上下级关系
467+
var currentSpan = DefaultSpan.Current?.ToString();
468+
if (!currentSpan.IsNullOrEmpty() && !Headers.ContainsKey("TraceId"))
469+
Headers["TraceId"] = currentSpan;
470+
471+
// 令牌与请求头一并注入参数集合。一次类型检查、一次 ToDictionary,避免重复转换
472+
if (args != null && (!Token.IsNullOrEmpty() || Headers.Count > 0))
470473
{
471-
var dic = args.ToDictionary();
472-
if (!dic.ContainsKey("Token")) dic["Token"] = Token;
473-
args = dic;
474+
var type = args.GetType();
475+
// 数组/列表类型不转换为字典,避免丢失原始数据
476+
if (!type.IsArray && !typeof(IList).IsAssignableFrom(type))
477+
{
478+
var dic = args.ToDictionary();
479+
480+
if (!Token.IsNullOrEmpty() && !dic.ContainsKey("Token"))
481+
dic["Token"] = Token;
482+
483+
foreach (var item in Headers)
484+
{
485+
if (!dic.ContainsKey(item.Key)) dic[item.Key] = item.Value;
486+
}
487+
488+
args = dic;
489+
}
474490
}
475491

476492
using var span = Tracer?.NewSpan("rpc:" + action, args);
@@ -676,15 +692,25 @@ public virtual async IAsyncEnumerable<TResult> InvokeStreamAsync<TResult>(String
676692
if (!currentSpan.IsNullOrEmpty() && !Headers.ContainsKey("TraceId"))
677693
Headers["TraceId"] = currentSpan;
678694

679-
// 注入 Headers
680-
if (Headers.Count > 0 && args != null)
695+
// 令牌与请求头一并注入参数集合。一次类型检查、一次 ToDictionary,避免重复转换
696+
if (args != null && (!Token.IsNullOrEmpty() || Headers.Count > 0))
681697
{
682-
var dic = args.ToDictionary();
683-
foreach (var item in Headers)
698+
var type = args.GetType();
699+
// 数组/列表类型不转换为字典,避免丢失原始数据
700+
if (!type.IsArray && !typeof(IList).IsAssignableFrom(type))
684701
{
685-
if (!dic.ContainsKey(item.Key)) dic[item.Key] = item.Value;
702+
var dic = args.ToDictionary();
703+
704+
if (!Token.IsNullOrEmpty() && !dic.ContainsKey("Token"))
705+
dic["Token"] = Token;
706+
707+
foreach (var item in Headers)
708+
{
709+
if (!dic.ContainsKey(item.Key)) dic[item.Key] = item.Value;
710+
}
711+
712+
args = dic;
686713
}
687-
args = dic;
688714
}
689715

690716
// 埋点,注入traceParent到参数集合

NewLife.Remoting/CloudProviders/AliyunClient.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Net.Http;
1+
using System.Collections;
2+
using System.Net.Http;
23
using System.Web;
34
using NewLife.Collections;
5+
using NewLife.Reflection;
46

57
namespace NewLife.Remoting.CloudProviders;
68

@@ -67,7 +69,16 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
6769
/// <returns></returns>
6870
protected override HttpRequestMessage BuildRequest(HttpMethod method, String action, Object? args, Type? returnType)
6971
{
70-
var parameters = args?.ToDictionary();
72+
Object? parameters = null;
73+
if (args != null)
74+
{
75+
var type = args.GetType();
76+
// 基础类型/数组/列表不能转为字典,直接传原始参数对象
77+
if (type.IsBaseType() || type.IsArray || typeof(IList).IsAssignableFrom(type))
78+
parameters = args;
79+
else
80+
parameters = args.ToDictionary();
81+
}
7182
var request = base.BuildRequest(method, action, parameters, returnType);
7283
if (request.Headers.Host.IsNullOrEmpty())
7384
request.Headers.Host = Endpoint;
@@ -95,7 +106,7 @@ protected override HttpRequestMessage BuildRequest(HttpMethod method, String act
95106

96107
// 2. 计算签名
97108
var signedHeaders = new List<String>();
98-
var canonicalRequest = AliyunClient.CreateCanonicalRequest(request, parameters, signedHeaders);
109+
var canonicalRequest = AliyunClient.CreateCanonicalRequest(request, parameters as IDictionary<String, Object?>, signedHeaders);
99110
var hashedRequest = canonicalRequest.GetBytes().SHA256().ToHex("").ToLowerInvariant();
100111
var stringToSign = $"{SignatureAlgorithm}\n{hashedRequest}";
101112
var signature = stringToSign.GetBytes().SHA256(AccessKeySecret.GetBytes()).ToHex("").ToLowerInvariant();

NewLife.Remoting/NewLife.Remoting.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
</ItemGroup>
5555

5656
<ItemGroup>
57-
<PackageReference Include="NewLife.Core" Version="11.17.2026.706-beta0929" />
57+
<PackageReference Include="NewLife.Core" Version="11.17.2026.714-beta0817" />
5858
</ItemGroup>
5959

6060
<ItemGroup>

NewLife.Remoting/WsClient.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#if !NET40
2+
using System.Collections;
23
using System.Net.WebSockets;
34
using NewLife.Collections;
45
using NewLife.Data;
@@ -243,9 +244,14 @@ public virtual Int32 InvokeOneWay(String action, Object? args = null, Byte flag
243244
// 令牌
244245
if (!Token.IsNullOrEmpty() && args != null)
245246
{
246-
var dic = args.ToDictionary();
247-
if (!dic.ContainsKey("Token")) dic["Token"] = Token;
248-
args = dic;
247+
var type = args.GetType();
248+
// 数组/列表类型不转换为字典,避免丢失原始数据
249+
if (!type.IsArray && !typeof(IList).IsAssignableFrom(type))
250+
{
251+
var dic = args.ToDictionary();
252+
if (!dic.ContainsKey("Token")) dic["Token"] = Token;
253+
args = dic;
254+
}
249255
}
250256

251257
// 埋点,注入traceParent到参数集合

Samples/IoTZero/IoTZero.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="NewLife.Cube.Core" Version="6.10.2026.404" />
26+
<PackageReference Include="NewLife.Cube.Core" Version="6.12.2026.708" />
2727
<PackageReference Include="NewLife.IoT" Version="3.0.2026.601" />
2828
<PackageReference Include="NewLife.MQTT" Version="3.1.2026.602" />
2929
<PackageReference Include="NewLife.Redis" Version="6.5.2026.701" />
30-
<PackageReference Include="NewLife.Stardust.Extensions" Version="3.8.2026.601" />
30+
<PackageReference Include="NewLife.Stardust.Extensions" Version="3.9.2026.708" />
3131
<PackageReference Include="NewLife.XCode" Version="12.0.2026.701" />
3232
</ItemGroup>
3333

Samples/Zero.Desktop/Zero.Desktop.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</PropertyGroup>
2727

2828
<ItemGroup>
29-
<PackageReference Include="NewLife.Stardust" Version="3.8.2026.601" />
29+
<PackageReference Include="NewLife.Stardust" Version="3.9.2026.708" />
3030
<PackageReference Include="System.Speech" Version="10.0.9" />
3131
</ItemGroup>
3232

Samples/Zero.RpcServer/Zero.RpcServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<ItemGroup>
2323
<PackageReference Include="NewLife.Redis" Version="6.5.2026.701" />
24-
<PackageReference Include="NewLife.Stardust" Version="3.8.2026.601" />
24+
<PackageReference Include="NewLife.Stardust" Version="3.9.2026.708" />
2525
<PackageReference Include="NewLife.XCode" Version="12.0.2026.701" />
2626
</ItemGroup>
2727

Samples/ZeroServer/ZeroServer.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
</PropertyGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="NewLife.Cube.Core" Version="6.10.2026.404" />
22+
<PackageReference Include="NewLife.Cube.Core" Version="6.12.2026.708" />
2323
<PackageReference Include="NewLife.Redis" Version="6.5.2026.701" />
24-
<PackageReference Include="NewLife.Stardust.Extensions" Version="3.8.2026.601" />
24+
<PackageReference Include="NewLife.Stardust.Extensions" Version="3.9.2026.708" />
2525
<PackageReference Include="NewLife.XCode" Version="12.0.2026.701" />
2626
</ItemGroup>
2727

Test/Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="NewLife.Core" Version="11.17.2026.706-beta0929" />
15+
<PackageReference Include="NewLife.Core" Version="11.17.2026.714-beta0817" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

0 commit comments

Comments
 (0)