I have a method ExecuteResult
, which is throwing a System.OutOfMemoryException
at the line Response.Write(sw.ToString())
. This is happening because the StringWriter
object is too large in memory for the ToString
; it fills up the memory.
I've been looking around for a solution but can't seem to find a simple clean solution to the problem. Any ideas would be greatly appreciated.
Code:
public class JsonNetResult : JsonResult
{
public JsonNetResult()
{
Settings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Error
};
}
public JsonSerializerSettings Settings { get; private set; }
public override void ExecuteResult(ControllerContext context)
{
if (this.Data != null)
{
if (context == null)
throw new ArgumentNullException("context");
if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException("JSON GET is not allowed");
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
if (this.ContentEncoding != null)
response.ContentEncoding = this.ContentEncoding;
var scriptSerializer = JsonSerializer.Create(this.Settings);
using (var sw = new StringWriter())
{
scriptSerializer.Serialize(sw, this.Data);
//outofmemory exception is happening here
response.Write(sw.ToString());
}
}
}
}
I think the problem is you are buffering all of the JSON into a StringWriter and then trying to write it out in one big chunk instead of streaming it out to the response.
Try replacing this code:
using (var sw = new StringWriter())
{
scriptSerializer.Serialize(sw, this.Data);
//outofmemory exception is happening here
response.Write(sw.ToString());
}
With this:
using (StreamWriter sw = new StreamWriter(response.OutputStream, ContentEncoding))
using (JsonTextWriter jtw = new JsonTextWriter(sw))
{
scriptSerializer.Serialize(jtw, this.Data);
}