0

I am try to return a custom object from VB.NET code behind to a AngularJS HTTP Post. Based on the user requests, this object could be very large. Very, very large. Whenever that happened, I got a OutOfMemoryException when trying to serialize it. Therefore, I am now using the JSON.NET package from Newtonsoft to serialze it using a StringWriter:

Using sw As New StringWriter()
    Using writer As JsonWriter = New JsonTextWriter(sw)
        Dim serializer As New JsonSerializer()
        serializer.Serialize(writer, periodData)
        writer.Flush()
    End Using
End Using

While this works, when I then try to do sw.ToString I still get that OutOfMemoryException.

So, I trawled through the internet and I believe I can use a StreamWriter sent to a HttpResponse object and then flush the response to return it to the web client call. However, I cannot figure out how to do this.

Any help? Or if I am way off track, give me a better way to do this?

Update 1

Apparently, I cannot use HttpResponse in a Using statement because I get a "using operand must implement system.idisposable" error. So now I am having trouble creating an HttpResponse object in the WebMethod. Passing both TextWriter and HttpWriter as params for the constructor give me errors.

Update 2

Okay, so I am no longer getting an OutOfMemoryException when trying to serialize the object. However, I believe I am doing something wrong since my StreamWriter doesn't seem to be writing to the HttpResponse Object. This is my current code:

Dim response As HttpResponse = HttpContext.Current.Response()
response.ContentType = "application/json"
response.Clear()
response.BufferOutput = True

Using sw As New StreamWriter(response.OutputStream, System.Text.Encoding.UTF8)
    Using writer As JsonWriter = New JsonTextWriter(sw)
        Dim serializer As New JsonSerializer()
        serializer.Serialize(writer, periodData)
        writer.Flush()
    End Using
    sw.Flush()
End Using

response.Flush()

Then when I pause my JavaScript in the Chrome developer console, the response object is just an empty string. The javascript is just a basic Angular $http.post call:

$http.post('url.aspx/GetData', angular.toJson({
    param1: data1,
    param2: data2,
    param3: data3
})).then(function (response) {
    //Do stuff with response which is currently an empty string.
}

I don't believe my VB.NET code is correct. Help?

1
  • That you are running out of memory is not quite your problem since you have identified a solution I can use a StreamWriter sent to a HttpResponse object and then flush the response to return it to the web client call. Why don't you try that? If you run into problems, we can help, but we can't write your code for you :) Commented Oct 7, 2016 at 18:42

1 Answer 1

0

It's not quite clear from your question where you are, but it looks like you are having trouble constructing a StreamWriter to write into a HttpResponse.

You can get the response object from the HttpContext:

[WebMethod]
public static void YourServiceMethod()
{
    HttpResponse response = HttpContext.Current.Response;

    // ... now go on
}

The response object has a property OutputStream that you can give to the constructor of StreamWriter (in a using statement), like in this answer to another question:

using (TextWriter textWriter = new StreamWriter(response.OutputStream, System.Text.Encoding.UTF8))
{
    // ... now write your data
}

(Excuse me giving C# samples, that's what I am much more familiar with.)

Sign up to request clarification or add additional context in comments.

2 Comments

How do I get the StreamWriter data into the HttpResponse object? Tried flushing the JsonWriter, StreamWriter, and HttpResponse object. None worked.
@MichaelWang The StreamWriter writes into the response's output stream, and you write into the StreamWriter - that's it, basically. --- You should really follow Verdolino's advice and put the essence of your current code into your question. It might be just a little detail that's wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.