Kestami
Kestami

Reputation: 2073

networkstream "cannot access a disposed object" when using newly created networkstream

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?

Upvotes: 2

Views: 5687

Answers (1)

Peter Ritchie
Peter Ritchie

Reputation: 35881

When a StreamWriter (and reader) is closed or its Dispose method 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 than StreamWriter or write a class to wrap the Stream given to StreamWriter and ignore the call to Dispose. in .NET 4.5, there is an overload you can use to tell the StreamWriter not to dispose the stream you give it. eg: new StreamWriter(stream, StreamWriter.UTF8NoBOM, 1024, false)

You could try using a wrapped stream to ignore the close (calling new StreamWriter(new NonDisposableStreamWrapper(stream))):

public class NonDisposableStreamWrapper : Stream
{
    private Stream wrappedStream;

    public NonDisposableStreamWrapper(Stream wrappedStream)
    {
        this.wrappedStream = wrappedStream;
    }

    public override void Flush()
    {
        wrappedStream.Flush();
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return wrappedStream.Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        wrappedStream.SetLength(value);
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return wrappedStream.Read(buffer, offset, count);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        wrappedStream.Write(buffer, offset, count);
    }

    public override bool CanRead
    {
        get { return wrappedStream.CanRead; }
    }

    public override bool CanSeek
    {
        get { return wrappedStream.CanSeek; }
    }

    public override bool CanWrite
    {
        get { return wrappedStream.CanWrite; }
    }

    public override long Length
    {
        get { return wrappedStream.Length; }
    }

    public override long Position
    {
        get { return wrappedStream.Position; }
        set { wrappedStream.Position = value; }
    }
}

Upvotes: 6

Related Questions