输出大Json对象和System.OutOfMemoryException

本文关键字:System OutOfMemoryException 对象 Json 输出 | 更新日期: 2023-09-27 18:14:15

我需要在ASP.NET(IIS7)应用程序中发送二进制数据(文件)作为JSON响应的响应,我使用来自System.Net的JsonObject对象。Json(与。net 4.x一起提供)。不幸的是,有时我得到内存不足的异常。

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Text.StringBuilder.ToString()
   at System.Net.Json.JsonUtility.EscapeString(String text)
   at System.Net.Json.JsonStringValue.WriteTo(TextWriter writer)
   at System.Net.Json.JsonCollection.WriteTo(TextWriter writer)
   at System.Net.Json.JsonObject.ToString()
   at System.Web.HttpWriter.Write(Object obj)
   at WebClient.Processor.FillResponse(HttpContext context, Object entity, OutputTypes outputType)
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid4[T0,T1,T2,T3](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at WebClient.Processor.ProcessRequest(HttpContext context)

的代码
JsonUtility.GenerateIndentedJsonText = true;
var jsonObject = new JsonObjectCollection();
context.Response.ContentType = JsonUtility.MimeType;
var jsonStringValue = new JsonStringValue("File", Convert.ToBase64String(binaryData, 0, binaryData.Length));
jsonObject.Add(jsonStringValue);
context.Response.Write(jsonObject);
有什么建议可以解决这个问题吗?

输出大Json对象和System.OutOfMemoryException

检查IIS中应用程序池的内存限制和回收时间设置。