当使用新创建的网络流时,networkstream“无法访问已处置的对象”

时间:2012-07-31 11:29:42

标签: c# .net winforms networkstream

我有几种不同的方法可以连接到远程主机,发送消息,获得回复并使用这些信息。这个工作正常,直到我在我的连接类的同一个实例中使用两个方法。在我收到错误的示例中,我运行以下方法;

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;
        }
    }
}

这发送正常,并按预期返回一条消息。但是,如果我然后使用我的连接类的相同实例并使用以下方法;

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
        }
    }
}

我收到的错误是“无法访问已处置的对象”;

 bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);

虽然我认为我刚刚用最新的方法分配了流。它试图使用之前关闭的吗?有什么方法可以解决这个或者我想念的傻事吗?

编辑:是否与客户端未正确处理?这两种方法是在一秒钟之内运行的,也许第二种方法是在第一次关闭之前尝试打开?

1 个答案:

答案 0 :(得分:6)

StreamWriter(和阅读器)关闭或调用其Dispose方法时,它会处理基础流。在.net 4.5之前,除了使用StreamWriter之外的其他内容或写一个类来将Stream包含给StreamWriter并忽略致电Dispose。在.NET 4.5中,有一个重载可以用来告诉StreamWriter不要处理你给它的流。例如:new StreamWriter(stream, StreamWriter.UTF8NoBOM, 1024, false)

您可以尝试使用包装的流来忽略关闭(调用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; }
    }
}