输出大型JSon对象和System.OutOfMemoryException

时间:2014-07-23 10:55:14

标签: c# asp.net .net json

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

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);

有任何建议如何解决问题?

1 个答案:

答案 0 :(得分:0)

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