Skip to content

Commit edf4668

Browse files
authored
Merge pull request #46 from softlgl/dev
优化序列化反序列化调用
2 parents 296dbeb + cc166e6 commit edf4668

4 files changed

Lines changed: 19 additions & 85 deletions

File tree

src/DotNetCoreRpc.Client/RequestHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private async Task<byte[]> SendRequest(MethodInfo methodInfo, params object[] ar
105105
Paramters = arguments
106106
};
107107

108-
HttpContent httpContent = new StringContent(requestModel.ToJson());
108+
HttpContent httpContent = new ByteArrayContent(requestModel.ToUtf8Bytes());
109109
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
110110

111111
string path = string.IsNullOrWhiteSpace(_httpClient.BaseAddress.PathAndQuery) || _httpClient.BaseAddress.PathAndQuery == "/"

src/DotNetCoreRpc.Core/JsonExtensions.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.IO;
23
using System.Text.Encodings.Web;
34
using System.Text.Json;
45
using System.Text.Json.Serialization;
6+
using System.Threading.Tasks;
57

68
namespace DotNetCoreRpc.Core
79
{
@@ -18,24 +20,29 @@ public static string ToJson<T>(this T data, JsonSerializerOptions options = defa
1820
return JsonSerializer.Serialize(data, options ?? serializerOptions);
1921
}
2022

21-
public static T FromJson<T>(this string json, JsonSerializerOptions options = default) where T : class, new()
23+
public static byte[] ToUtf8Bytes<T>(this T data, JsonSerializerOptions options = default) where T : class, new()
2224
{
23-
return JsonSerializer.Deserialize<T>(json, options ?? serializerOptions);
25+
return JsonSerializer.SerializeToUtf8Bytes(data, options ?? serializerOptions);
2426
}
2527

26-
public static object FromJson(this string json, Type type, JsonSerializerOptions options = default)
28+
public static Task WriteToStream<T>(this T data, Stream utf8Json, JsonSerializerOptions options = default) where T : class, new()
2729
{
28-
return JsonSerializer.Deserialize(json, type, options ?? serializerOptions);
30+
return JsonSerializer.SerializeAsync(utf8Json, data, options ?? serializerOptions);
31+
}
32+
33+
public static T FromJson<T>(this string json, JsonSerializerOptions options = default) where T : class, new()
34+
{
35+
return JsonSerializer.Deserialize<T>(json, options ?? serializerOptions);
2936
}
3037

3138
public static T FromJson<T>(this byte[] utf8Json, JsonSerializerOptions options = default) where T : class, new()
3239
{
3340
return JsonSerializer.Deserialize<T>(utf8Json, options ?? serializerOptions);
3441
}
3542

36-
public static object FromJson(this byte[] utf8Json, Type type, JsonSerializerOptions options = default)
43+
public static ValueTask<T> FromStream<T>(this Stream utf8Json, JsonSerializerOptions options = default) where T : class, new()
3744
{
38-
return JsonSerializer.Deserialize(utf8Json, type, options ?? serializerOptions);
45+
return JsonSerializer.DeserializeAsync<T>(utf8Json, options ?? serializerOptions);
3946
}
4047

4148
public static T FromJson<T>(this JsonElement jsonElement, JsonSerializerOptions options = default) where T : class, new()

src/DotNetCoreRpc.Server/DotNetCoreRpcMiddleware.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,13 @@ public DotNetCoreRpcMiddleware(RequestDelegate _, RpcServerOptions rpcServerOpti
2828

2929
public async Task InvokeAsync(HttpContext context)
3030
{
31-
var requestContent = await context.Request.ReadStringAsync();
32-
ResponseModel responseModel = new ResponseModel { Code = (int)HttpStatusCode.InternalServerError };
33-
if (string.IsNullOrEmpty(requestContent))
34-
{
35-
responseModel.Message = "未读取到请求信息";
36-
await context.Response.WriteAsync(responseModel.ToJson());
37-
return;
38-
}
31+
context.Request.EnableBuffering();
3932

40-
RequestModel requestModel = requestContent.FromJson<RequestModel>();
33+
RequestModel requestModel = await context.Request.Body.FromStream<RequestModel>();
4134
if (requestModel == null)
4235
{
43-
responseModel.Message = "读取请求数据失败";
44-
await context.Response.WriteAsync(responseModel.ToJson());
36+
ResponseModel responseModel = new ResponseModel { Code = (int)HttpStatusCode.InternalServerError, Message = "读取请求数据失败" };
37+
await responseModel.WriteToStream(context.Response.Body);
4538
return;
4639
}
4740

@@ -110,7 +103,7 @@ private AspectPiplineBuilder CreatPipleline(RpcContext aspectContext)
110103
responseModel = returnValue;
111104
}
112105

113-
await aspectContext.HttpContext.Response.WriteAsync(responseModel.ToJson());
106+
await responseModel.WriteToStream(rpcContext.HttpContext.Response.Body);
114107
});
115108

116109
List<RpcFilterAttribute> interceptorAttributes = RpcFilterUtils.GetFilterAttributes(aspectContext, _serviceProvider, _rpcServerOptions.GetFilterTypes());

src/DotNetCoreRpc.Server/HttpRequestExtensions.cs

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)