I use the code below on a thread (showing a progress dialog in ui) to read json string from a ASP.Net MVC web service. The data can be between 1 mb and 4 mb.
public static class WebRequestEx
{
public static string ExecuteRequestReadToEnd(this WebRequest req)
{
var resp = req.GetResponse();
using (var resps = resp.GetResponseStream())
{
StringBuilder sb = new StringBuilder();
// read through the stream loading up the string builder
using (var respRdr = new StreamReader(resps))
{
//return respRdr.ReadToEnd();
while (!respRdr.EndOfStream)
{
sb.Append(respRdr.ReadLine());
}
return sb.ToString();
}
}
}
}
The stacktrace is as follows.
I/mono (15666): Stacktrace:
I/mono (15666):
I/mono (15666): at System.Threading.WaitHandle.set_Handle (intptr) <0x0008b>
I/mono (15666): at System.Threading.EventWaitHandle..ctor (bool,System.Threading.EventResetMode) <0x00053>
I/mono (15666): at System.Threading.ManualResetEvent..ctor (bool) <0x0001f>
I/mono (15666): at (wrapper remoting-invoke-with-check) System.Threading.ManualResetEvent..ctor (bool) <0xffffffff>
I/mono (15666): at System.Net.WebAsyncResult.get_AsyncWaitHandle () <0x00073>
I/mono (15666): at System.Net.WebAsyncResult.WaitUntilComplete (int,bool) <0x00033>
I/mono (15666): at System.Net.WebConnectionStream.Read (byte[],int,int) <0x000b3>
I/mono (15666): at System.IO.StreamReader.ReadBuffer () <0x00047>
I/mono (15666): at System.IO.StreamReader.ReadLine () <0x0014b>
I/mono (15666): at System.WebRequestEx.ExecuteRequestReadToEnd (System.Net.WebRequest) <0x000ab>
The issue is that this does not happen every time and is intermittent. This is bad because it causes the whole app to freeze and progress dialog is stuck with the user unable to do anything with the app. I do have a try/catch block in the calling code but this exception seems to get around it.
I was using ReadToEnd earlier and that was blowing up intermittently as well so I switched to reading line by line.
The stack trace is not particularly helpful as somehting seems to be going on within Readline().
Thoughts/advice/alternatives?