2

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

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

虽然我认为我刚刚以最新的方法分配了流。它是否试图使用以前关闭的?有没有办法解决这个问题或我错过了什么愚蠢的事情?

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

4

1 回答 1

6

当一个StreamWriter(和阅读器)关闭或Dispose调用它的方法时,它会释放底层流。在 .net 4.5 之前,除了使用其他东西StreamWriter或编写一个类来包装Stream给定的 toStreamWriter并忽略对Dispose. 在 .NET 4.5 中,您可以使用一个重载来告诉StreamWriter不要处理您给它的流。例如:新的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; }
    }
}
于 2012-07-31T14:07:11.000 回答