c# 使用 JSON 序列化大对象
c# Serializing large objects using JSON
我在 JSON 序列化方面遇到了一个大问题。
在我的应用程序中,我使用 Web API 控制器之一定义了以下函数。
[HttpGet]
[ActionName("all")]
[Route("api/customers/all/")]
[SwaggerResponse(HttpStatusCode.InternalServerError, Type = typeof(Models.Response.ResponseError))]
[SwaggerResponse(HttpStatusCode.Forbidden, Type = typeof(Models.Response.ResponseErrorBasic))]
public Models.Response.ResponseApiList<Models.Api.Customer> GetValuesAll(DateTime? since=null)
Models.Api.Customer 是一个引用几个相关对象的复杂对象。
public class Customer
{
public int Id { get;set;}
public string Name {get;set}
.....
public List<Invoices> Invoices {get;set;}
.....
.....
}
Models.Response.ResponseApiList保存返回的数据
public class ResponseApiList<T>
{
public ResponseMeta meta { get; set; } // ResponseMeta holds datetime and total records
public List<T> payload { get; set; } // Holds the Customer object
public ResponseApiList()
{
payload = new List<T>();
meta = new ResponseMeta();
}
最后在WebApiConfig中有如下内容
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
NewtonSoft.JSON 库已加载,但我不确定要查看它是否在使用中。
总之,是性能问题。目前,大约有 11,000 条记录从数据库返回以供处理,但数据的总大小(根据 fiddler 告诉我的)是 45Mbs。
当函数中的 c# 代码 returns 时,所用的总时间约为。 4 秒。
然后,很长一段时间都没有。今天我在 11:43 开始了这个过程,直到 15:34 才停止。我的测试工具有 ReadyAPI (SoapUI) 和 Insomnia.
最终,这将被转换为分页调用以减少发送的数据量,但是从 Web API 方法发送回 JSON 的限制是什么?
在我走上重新编码路线之前,有没有人有其他方法可以做到这一点?
- 首先 - 发送如此巨大的响应 (45Mb) 是非常奇怪的想法。当这么大的数据对象有用时,我无法发明任何情况。
- 如果您有更大的项目集合 - 最好的方法是分页输出。 Limit/offset 例如。
Ultimately, this is going to be converted into a paged call to reduce the amount of data sent, but what are the limits of sending JSON back from a Web API method?
我相信对响应大小没有严格的 Web-API 级限制。特别是这个问题与 json-serializer 无关。这种超大响应场景中的限制很可能会出现在网络服务器配置级别上。 (诸如最大响应大小等)。
但是这种情况的主要结论应该是——"If json object is 45Mb large - something goes wrong."
我在 JSON 序列化方面遇到了一个大问题。
在我的应用程序中,我使用 Web API 控制器之一定义了以下函数。
[HttpGet]
[ActionName("all")]
[Route("api/customers/all/")]
[SwaggerResponse(HttpStatusCode.InternalServerError, Type = typeof(Models.Response.ResponseError))]
[SwaggerResponse(HttpStatusCode.Forbidden, Type = typeof(Models.Response.ResponseErrorBasic))]
public Models.Response.ResponseApiList<Models.Api.Customer> GetValuesAll(DateTime? since=null)
Models.Api.Customer 是一个引用几个相关对象的复杂对象。
public class Customer
{
public int Id { get;set;}
public string Name {get;set}
.....
public List<Invoices> Invoices {get;set;}
.....
.....
}
Models.Response.ResponseApiList保存返回的数据
public class ResponseApiList<T>
{
public ResponseMeta meta { get; set; } // ResponseMeta holds datetime and total records
public List<T> payload { get; set; } // Holds the Customer object
public ResponseApiList()
{
payload = new List<T>();
meta = new ResponseMeta();
}
最后在WebApiConfig中有如下内容
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
NewtonSoft.JSON 库已加载,但我不确定要查看它是否在使用中。
总之,是性能问题。目前,大约有 11,000 条记录从数据库返回以供处理,但数据的总大小(根据 fiddler 告诉我的)是 45Mbs。
当函数中的 c# 代码 returns 时,所用的总时间约为。 4 秒。
然后,很长一段时间都没有。今天我在 11:43 开始了这个过程,直到 15:34 才停止。我的测试工具有 ReadyAPI (SoapUI) 和 Insomnia.
最终,这将被转换为分页调用以减少发送的数据量,但是从 Web API 方法发送回 JSON 的限制是什么?
在我走上重新编码路线之前,有没有人有其他方法可以做到这一点?
- 首先 - 发送如此巨大的响应 (45Mb) 是非常奇怪的想法。当这么大的数据对象有用时,我无法发明任何情况。
- 如果您有更大的项目集合 - 最好的方法是分页输出。 Limit/offset 例如。
Ultimately, this is going to be converted into a paged call to reduce the amount of data sent, but what are the limits of sending JSON back from a Web API method?
我相信对响应大小没有严格的 Web-API 级限制。特别是这个问题与 json-serializer 无关。这种超大响应场景中的限制很可能会出现在网络服务器配置级别上。 (诸如最大响应大小等)。
但是这种情况的主要结论应该是——"If json object is 45Mb large - something goes wrong."