|
8 | 8 | using NewLife.Remoting.Http; |
9 | 9 | using NewLife.Serialization; |
10 | 10 | using NewLife.Threading; |
| 11 | +using System.Collections; |
11 | 12 | using System.Collections.Concurrent; |
12 | 13 | using System.Runtime.CompilerServices; |
13 | 14 | #if !NET40 |
@@ -308,31 +309,28 @@ public virtual Int32 InvokeOneWay(String action, Object? args = null, Byte flag |
308 | 309 |
|
309 | 310 | LastActive = DateTime.Now; |
310 | 311 |
|
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 | | - |
319 | 312 | // 自动注入当前 Span 上下文字符串到 Headers,包含 TraceId+SpanId 便于星尘构建调用链上下级关系 |
320 | 313 | var currentSpan = DefaultSpan.Current?.ToString(); |
321 | 314 | if (!currentSpan.IsNullOrEmpty() && !Headers.ContainsKey("TraceId")) |
322 | 315 | Headers["TraceId"] = currentSpan; |
323 | 316 |
|
324 | | - // Headers 注入 |
325 | | - if (Headers.Count > 0 && args != null) |
| 317 | + // 令牌与请求头一并注入参数集合。一次类型检查、一次 ToDictionary,避免重复转换 |
| 318 | + if (args != null && (!Token.IsNullOrEmpty() || Headers.Count > 0)) |
326 | 319 | { |
327 | 320 | var type = args.GetType(); |
328 | 321 | // 数组/列表类型不转换为字典,避免丢失原始数据 |
329 | | - if (!type.IsArray && !typeof(System.Collections.IList).IsAssignableFrom(type)) |
| 322 | + if (!type.IsArray && !typeof(IList).IsAssignableFrom(type)) |
330 | 323 | { |
331 | 324 | var dic = args.ToDictionary(); |
| 325 | + |
| 326 | + if (!Token.IsNullOrEmpty() && !dic.ContainsKey("Token")) |
| 327 | + dic["Token"] = Token; |
| 328 | + |
332 | 329 | foreach (var item in Headers) |
333 | 330 | { |
334 | 331 | if (!dic.ContainsKey(item.Key)) dic[item.Key] = item.Value; |
335 | 332 | } |
| 333 | + |
336 | 334 | args = dic; |
337 | 335 | } |
338 | 336 | } |
@@ -465,12 +463,30 @@ public Int32 InvokeWithClient(ISocketClient client, String action, Object? args, |
465 | 463 |
|
466 | 464 | LastActive = DateTime.Now; |
467 | 465 |
|
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)) |
470 | 473 | { |
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 | + } |
474 | 490 | } |
475 | 491 |
|
476 | 492 | using var span = Tracer?.NewSpan("rpc:" + action, args); |
@@ -676,15 +692,25 @@ public virtual async IAsyncEnumerable<TResult> InvokeStreamAsync<TResult>(String |
676 | 692 | if (!currentSpan.IsNullOrEmpty() && !Headers.ContainsKey("TraceId")) |
677 | 693 | Headers["TraceId"] = currentSpan; |
678 | 694 |
|
679 | | - // 注入 Headers |
680 | | - if (Headers.Count > 0 && args != null) |
| 695 | + // 令牌与请求头一并注入参数集合。一次类型检查、一次 ToDictionary,避免重复转换 |
| 696 | + if (args != null && (!Token.IsNullOrEmpty() || Headers.Count > 0)) |
681 | 697 | { |
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)) |
684 | 701 | { |
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; |
686 | 713 | } |
687 | | - args = dic; |
688 | 714 | } |
689 | 715 |
|
690 | 716 | // 埋点,注入traceParent到参数集合 |
|
0 commit comments