ArgumentOutOfRangeException and ObjectDisposedException in StreamReader.ReadToEnd()

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
 
J

Jason Sagar

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

Patrik Kruse

I still very much would like some suggestions how to get rid of these
errors...

/Patrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top