diff --git a/NewLife.Remoting/ApiClient.cs b/NewLife.Remoting/ApiClient.cs
index 9e9d24f..7503357 100644
--- a/NewLife.Remoting/ApiClient.cs
+++ b/NewLife.Remoting/ApiClient.cs
@@ -5,6 +5,7 @@
using NewLife.Model;
using NewLife.Net;
using NewLife.Remoting.Http;
+using NewLife.Serialization;
using NewLife.Threading;
#if !NET40
using TaskEx = System.Threading.Tasks.Task;
@@ -46,6 +47,9 @@ public class ApiClient : ApiHost, IApiClient
/// 收到请求或响应时
public event EventHandler? Received;
+ /// 服务提供者。创建控制器实例时使用,可实现依赖注入。务必在注册控制器之前设置该属性
+ public IServiceProvider? ServiceProvider { get; set; }
+
/// 调用统计
public ICounter? StatInvoke { get; set; }
#endregion
@@ -89,7 +93,8 @@ public virtual Boolean Open()
var ss = Servers;
if (ss == null || ss.Length == 0) throw new ArgumentNullException(nameof(Servers), "未指定服务端地址");
- Encoder ??= new JsonEncoder();
+ var json = ServiceProvider?.GetService() ?? JsonHelper.Default;
+ Encoder ??= new JsonEncoder { JsonHost = json };
// 集群
Cluster = InitCluster();
diff --git a/NewLife.Remoting/ApiHttpServer.cs b/NewLife.Remoting/ApiHttpServer.cs
index a2914c2..1a2673c 100644
--- a/NewLife.Remoting/ApiHttpServer.cs
+++ b/NewLife.Remoting/ApiHttpServer.cs
@@ -1,6 +1,7 @@
using NewLife.Http;
+using NewLife.Model;
using NewLife.Net;
-using NewLife.Remoting.Http;
+using NewLife.Serialization;
using HttpCodec = NewLife.Remoting.Http.HttpCodec;
namespace NewLife.Remoting;
@@ -29,13 +30,15 @@ public override Boolean Init(Object config, IApiHost host)
if (config is NetUri uri) Port = uri.Port;
//RawUrl = uri + "";
+ var json = ServiceProvider?.GetService() ?? JsonHelper.Default;
// Http封包协议
//Add();
- Add(new HttpCodec { AllowParseHeader = true });
+ Add(new HttpCodec { AllowParseHeader = true, JsonHost = json });
//host.Handler = new ApiHttpHandler { Host = host };
- host.Encoder = new HttpEncoder();
+
+ host.Encoder = new HttpEncoder { JsonHost = json };
return true;
}
diff --git a/NewLife.Remoting/ApiNetServer.cs b/NewLife.Remoting/ApiNetServer.cs
index 6d66deb..2386b29 100644
--- a/NewLife.Remoting/ApiNetServer.cs
+++ b/NewLife.Remoting/ApiNetServer.cs
@@ -1,8 +1,9 @@
-using NewLife.Http;
-using NewLife.Log;
+using NewLife.Log;
using NewLife.Messaging;
+using NewLife.Model;
using NewLife.Net;
using NewLife.Remoting.Http;
+using NewLife.Serialization;
using HttpCodec = NewLife.Remoting.Http.HttpCodec;
namespace NewLife.Remoting;
@@ -38,9 +39,10 @@ public virtual Boolean Init(Object config, IApiHost host)
// Add();
//else
// Add(new HttpCodec { AllowParseHeader = true });
-
+ var json = ServiceProvider?.GetService() ?? JsonHelper.Default;
+
Add();
- Add(new HttpCodec { AllowParseHeader = true });
+ Add(new HttpCodec { AllowParseHeader = true, JsonHost = json });
// 新生命标准网络封包协议
Add(Host.GetMessageCodec());
diff --git a/NewLife.Remoting/ApiServer.cs b/NewLife.Remoting/ApiServer.cs
index 91488e8..0d1644f 100644
--- a/NewLife.Remoting/ApiServer.cs
+++ b/NewLife.Remoting/ApiServer.cs
@@ -5,6 +5,7 @@
using NewLife.Messaging;
using NewLife.Model;
using NewLife.Net;
+using NewLife.Serialization;
using NewLife.Threading;
namespace NewLife.Remoting;
@@ -47,7 +48,7 @@ public class ApiServer : ApiHost, IServer
public event EventHandler? Received;
/// 服务提供者。创建控制器实例时使用,可实现依赖注入。务必在注册控制器之前设置该属性
- public IServiceProvider? ServiceProvider { get; set; } //= ObjectContainer.Provider;
+ public IServiceProvider? ServiceProvider { get; set; }
/// 处理统计
public ICounter? StatProcess { get; set; }
@@ -167,7 +168,8 @@ public virtual void Start()
{
if (Active) return;
- Encoder ??= new JsonEncoder();
+ var json = ServiceProvider?.GetService() ?? JsonHelper.Default;
+ Encoder ??= new JsonEncoder { JsonHost = json };
Handler ??= new ApiHandler { Host = this };
Encoder.Log = EncoderLog;
diff --git a/NewLife.Remoting/Http/HttpCodec.cs b/NewLife.Remoting/Http/HttpCodec.cs
index 399a165..b853d19 100644
--- a/NewLife.Remoting/Http/HttpCodec.cs
+++ b/NewLife.Remoting/Http/HttpCodec.cs
@@ -1,10 +1,8 @@
-using System;
-using System.Collections.Generic;
-using NewLife.Data;
+using NewLife.Data;
using NewLife.Http;
-using NewLife.Messaging;
using NewLife.Model;
using NewLife.Net;
+using NewLife.Serialization;
namespace NewLife.Remoting.Http;
@@ -17,6 +15,9 @@ public class HttpCodec : Handler
/// 分析头部对性能有一定损耗
///
public Boolean AllowParseHeader { get; set; }
+
+ /// Json主机。提供序列化能力
+ public IJsonHost JsonHost { get; set; } = JsonHelper.Default;
#endregion
/// 写入数据
@@ -58,7 +59,7 @@ public class HttpCodec : Handler
// 第一个请求必须是GET/POST,才执行后续操作
if (!isGet && !isPost) return base.Read(context, message);
- ext["Encoder"] = new HttpEncoder();
+ ext["Encoder"] = new HttpEncoder { JsonHost = JsonHost };
}
// 检查是否有未完成消息
diff --git a/NewLife.Remoting/Http/HttpEncoder.cs b/NewLife.Remoting/Http/HttpEncoder.cs
index c08784d..2e879c7 100644
--- a/NewLife.Remoting/Http/HttpEncoder.cs
+++ b/NewLife.Remoting/Http/HttpEncoder.cs
@@ -13,6 +13,9 @@ namespace NewLife.Http;
public class HttpEncoder : EncoderBase, IEncoder
{
#region 属性
+ /// Json主机。提供序列化能力
+ public IJsonHost JsonHost { get; set; } = JsonHelper.Default;
+
/// 是否使用Http状态。默认false,使用json包装响应码
public Boolean UseHttpStatus { get; set; }
#endregion
@@ -34,11 +37,9 @@ public class HttpEncoder : EncoderBase, IEncoder
if (value == null) return null;
- String json;
- if (UseHttpStatus)
- json = value.ToJson(false, false, false);
- else
- json = new { action, code, data = value }.ToJson(false, true, false);
+ var json = UseHttpStatus ?
+ JsonHost.Write(value, false, false, false) :
+ JsonHost.Write(new { action, code, data = value }, false, true, false);
WriteLog("{0}=>{1}", action, json);
return json.GetBytes();
@@ -124,7 +125,7 @@ public class HttpEncoder : EncoderBase, IEncoder
///
///
///
- public virtual Object? Convert(Object obj, Type targetType) => JsonHelper.Default.Convert(obj, targetType);
+ public virtual Object? Convert(Object obj, Type targetType) => JsonHost.Convert(obj, targetType);
#region 编码/解码
/// 创建请求
diff --git a/NewLife.Remoting/JsonEncoder.cs b/NewLife.Remoting/JsonEncoder.cs
index 318e8fa..8c6651e 100644
--- a/NewLife.Remoting/JsonEncoder.cs
+++ b/NewLife.Remoting/JsonEncoder.cs
@@ -1,5 +1,6 @@
using NewLife.Data;
using NewLife.Messaging;
+using NewLife.Model;
using NewLife.Reflection;
using NewLife.Serialization;
@@ -8,6 +9,9 @@ namespace NewLife.Remoting;
/// Json编码器
public class JsonEncoder : EncoderBase, IEncoder
{
+ /// Json主机。提供序列化能力
+ public IJsonHost JsonHost { get; set; } = JsonHelper.Default;
+
/// 编码。请求/响应
///
///
@@ -86,7 +90,7 @@ public virtual Packet Encode(String action, Int32? code, Packet? value)
///
///
///
- public Object? Convert(Object obj, Type targetType) => JsonHelper.Default.Convert(obj, targetType);
+ public Object? Convert(Object obj, Type targetType) => JsonHost.Convert(obj, targetType);
/// 创建请求
///
@@ -154,7 +158,7 @@ public IMessage CreateResponse(IMessage msg, String action, Int32 code, Object?
if (value is Exception ex)
value = str = ex.GetTrue().Message;
else
- str = value.ToJson(false, false, false);
+ str = JsonHost.Write(value, false, false, false);
pk = str.GetBytes();
}
diff --git a/Samples/IoTZero/Common/ApiFilterAttribute.cs b/Samples/IoTZero/Common/ApiFilterAttribute.cs
index 44e4af4..616e24d 100644
--- a/Samples/IoTZero/Common/ApiFilterAttribute.cs
+++ b/Samples/IoTZero/Common/ApiFilterAttribute.cs
@@ -37,30 +37,31 @@ public override void OnActionExecuted(ActionExecutedContext context)
if (context.Result != null)
if (context.Result is ObjectResult obj)
{
+ var host = context.HttpContext.RequestServices?.GetService() ?? JsonHelper.Default;
//context.Result = new JsonResult(new { code = obj.StatusCode ?? 0, data = obj.Value });
var rs = new { code = obj.StatusCode ?? 0, data = obj.Value, traceId };
context.Result = new ContentResult
{
- Content = rs.ToJson(false, true, true),
+ Content = host.Write(rs, false, true, true),
ContentType = "application/json",
StatusCode = 200
};
}
else if (context.Result is EmptyResult)
context.Result = new JsonResult(new { code = 0, data = new { }, traceId });
- else if (context.Exception != null && !context.ExceptionHandled)
- {
- var ex = context.Exception.GetTrue();
- if (ex is NewLife.Remoting.ApiException aex)
- context.Result = new JsonResult(new { code = aex.Code, data = aex.Message, traceId });
- else
- context.Result = new JsonResult(new { code = 500, data = ex.Message, traceId });
+ else if (context.Exception != null && !context.ExceptionHandled)
+ {
+ var ex = context.Exception.GetTrue();
+ if (ex is NewLife.Remoting.ApiException aex)
+ context.Result = new JsonResult(new { code = aex.Code, data = aex.Message, traceId });
+ else
+ context.Result = new JsonResult(new { code = 500, data = ex.Message, traceId });
- context.ExceptionHandled = true;
+ context.ExceptionHandled = true;
- // 输出异常日志
- if (XTrace.Debug) XTrace.WriteException(ex);
- }
+ // 输出异常日志
+ if (XTrace.Debug) XTrace.WriteException(ex);
+ }
base.OnActionExecuted(context);
}