Bizarre ObjectDisposedException - Please help

C

Chris Hudson

Any help you can offer on this would be most welcome. Its making me pull
out what little hair I have left!

A strange exception is raised occasionally by the following code:



byte[] buffer = new ASCIIEncoding().GetBytes(toSend);

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(new
Uri(serviceUri));

httpWebRequest.ContentLength = toSend.Length;
httpWebRequest.ContentType = postType;
httpWebRequest.Method = "POST";

Stream requestStream = httpWebRequest.GetRequestStream();

requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();

StreamReader responseStream = new
StreamReader(httpWebRequest.GetResponse().GetResponseStream());

string received = responseStream.ReadToEnd();

responseStream.Close();



It only seems to happen the first time after the code has been rebuilt, or
has not been called for some time (a number of hours). If the code is
regularly called, then the exception is not raised.

The code is in a C# WebService. I am using .NET Framework 1.1 on a Windows
XP (SP2) machine.



The exception message is:

Exception:-
System.ObjectDisposedException: Cannot access a disposed object named
"System.Net.TlsStream".
Object name: "System.Net.TlsStream".
at System.Net.TlsStream.InnerWrite(Boolean async, Byte[] buffer, Int32
offset, Int32 size, AsyncCallback asyncCallback, Object asyncState)
at System.Net.TlsStream.BeginWrite(Byte[] buffer, Int32 offset, Int32
size, AsyncCallback asyncCallback, Object asyncState)
at System.Net.Connection.BeginWrite(Byte[] buffer, Int32 offset, Int32
size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.BeginWrite(Byte[] buffer, Int32 offset, Int32
size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32
size)
 
P

Peter Huang

Hi Chris,

Which code line cause the exception?
If you can not run it in the debug mode, I think you may try to write to
logfile after every method call which will help us to identify where the
exception occur.
Also as a suggsetion, I think you may try to move the code line below
requestStream.Close();
after the code.
responseStream.Close();

That is to say, close the requestStream after the responseSteam.close();

You may have a try and let me know the result.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chris Hudson

Thanks for your help Peter.

I have moved the line of code:

requestStream.Close(); after responseStream.Close();

as you suggested. I have also put in a line of code to write to the
eventlog after each method call to try and discover exactly which line of
code is raising the exception.

I have done some initial testing with these changes to the code and so far
the exception has not been raised. I will continue to monitor the situation
and let you know if and when the exception is raised again.

Once again, thanks for your help,

Chris
 
C

Chris Hudson

I have tested the code all day and it has not failed once. Although the
problem was intermittent before I changed the code, I would normally have
seen 3-4 occurences of it in days testing.

So, I think you have fixed the problem for me! Many thanks.

Chris
 
P

Peter Huang

Hi Chris,

I am glad that the problem is resolved.
Cheers!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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