Network stream error "Cannot close stream until all bytes are written"

时间:2019-04-23 15:07:28

标签: c# sockets networkstream

The code below occasionally blows and it's because the using block exits causing a dispose on the "stream" object before the stream has been written fully.

private void DoStuff(Stream input) 
{
    using (var stream = _httpWebRequest.GetRequestStream())
    {
        var buffer = new byte[input.Length];
        input.Read(buffer, 0, buffer.Length);
        stream.Write(buffer, 0, buffer.Length);
        stream.Flush();
    }
}

The resulting error is: The request was aborted: The request was canceled. ---> System.IO.IOException: Cannot close stream until all bytes are written.

0 个答案:

没有答案