I have a few different methods which connect to a remote host, send a message, get a reply, and use the information. this has worked fine until i use two methods in the same instance of my connection class. in the example where i’m getting the error, i run the following method;
public string sendRequestAccountID(string siteID)
{
//build message
String response = String.Empty;
TcpClient client = new TcpClient();
client.Connect(detailsHere);
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream);
writer.AutoFlush = false;
writer.WriteLine(sb.ToString());
writer.Flush();
StreamReader reader = new StreamReader(stream);
List<XmlNode> nodeList = new List<XmlNode>();
byte[] responseBytes = new byte[4096];
int bytesRead = 0;
while (true)
{
bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);
if (bytesRead > 0)
{
//handle message
}
if (bytesRead == 0)
{
stream.Flush();
stream.Close();
client.Close();
string finalResponse = stuffHereToSend;
return finalResponse;
}
}
}
This sends fine, and returns a message as expected. However, if i then use the same instance of my connection class and use the following method;
public bool sendNewDevice(IDeviceInterface device)
{
NetworkStream stream;
sb = new StringBuilder();
//build message
String response = String.Empty;
TcpClient client = new TcpClient();
client.Connect(detailsHere);
stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream);
writer.AutoFlush = false;
writer.WriteLine(sb.ToString());
writer.Flush();
StreamReader reader = new StreamReader(stream);
List<XmlNode> nodeList = new List<XmlNode>();
byte[] responseBytes = new byte[4096];
int bytesRead = 0;
while (true)
{
bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);
if (bytesRead > 0)
{
//handle message
}
}
}
I’m getting an error which reads “cannot access a disposed object” on;
bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);
Although i thought that i’d newly just assigned the stream in the latest method. Is it trying to use the previously closed one? is there a way around this or something silly that i’m missing?
edit: is it something to with the clients not disposing correctly? the two methods are ran within a second of each other, maybe the second is trying to open before the first has closed?
When a
StreamWriter(and reader) is closed or itsDisposemethod is called, it disposes the underlying stream. Prior to .net 4.5 there really wasn’t anything you could do about it, other than use something other thanStreamWriteror write a class to wrap theStreamgiven toStreamWriterand ignore the call toDispose. in .NET 4.5, there is an overload you can use to tell theStreamWriternot to dispose the stream you give it. eg: newStreamWriter(stream, StreamWriter.UTF8NoBOM, 1024, false)You could try using a wrapped stream to ignore the close (calling
new StreamWriter(new NonDisposableStreamWrapper(stream))):