ArgumentOutOfRangeException and ObjectDisposedException in StreamReader.ReadToEnd()

  • Thread starter Thread starter Patrik Kruse
  • Start date Start date
P

Patrik Kruse

Most times the method below are performing al right. Sometimes however (and
much too often) the call to ReadToEnd results in one of the these
exceptions:

- System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: byteCount

- System.ObjectDisposedException: Cannot access a disposed object named
System.Net.Sockets.NetworkStream. Object name:
System.Net.Sockets.NetworkStream

I am using dotnet framwork 1.1. The application is heavily multithreaded and
runs under heavy load. My best guess is that the error is somehow connected
to the character encoding, but I really don't know...

Any suggestions?


Here comes the C#-code:

public string GetHttpWebRequestText(HttpWebRequest request)

{
string result;
HttpWebResponse response = null;
Stream receiveStream = null;
StreamReader readStream = null;
try
{
response = (HttpWebResponse) request.GetResponse();
cookies=ExtractCookieString(response.Headers.ToString());
receiveStream = response.GetResponseStream();
readStream = new StreamReader(receiveStream, Encoding.UTF8);
result = readStream.ReadToEnd();

readStream.Close();
readStream = null;

receiveStream.Close();
receiveStream = null;

response.Close();
response = null;

return result;
}
catch(Exception ex)
{
throw new Exception("Error getting response", ex);
}
finally
{
if (readStream != null)
{
readStream.Close();
readStream = null;
}
if (receiveStream != null)
{
receiveStream.Close();
receiveStream = null;
}

if (response != null)
{
response.Close();
response = null;
}
}
}


/Patrik Kruse
 
You probably accidentally closed the Stream object you are using.

Can you post the piece of code you are running? (e.g. where the Stream
object gets created and released)
 
I still very much would like some suggestions how to get rid of these
errors...

/Patrik
 
Back
Top