gpt4 book ai didi

c# - 取消 WriteAsync 操作时为 "Cannot close stream until all bytes are written"

转载 作者:行者123 更新时间:2023-11-30 12:58:40 32 4
gpt4 key购买 nike

HttpWebRequest.GetRequestStream() 创建的流上取消 WriteAsync 操作时,我收到一个 WebException 并显示以下消息:

Cannot close stream until all bytes are written

当我使用 CancellationToken 取消操作时,如何清理我的 HttpWebRequest 和流?

这是我工作代码的一部分(我为这篇文章清理了一些标题,因此请求可能格式不正确):

    public async Task<bool> UploadAsync(FileInfo fi, CancellationToken ct, IProgress<int> progress = null)
{
FileStream fs = new FileStream(fi.FullName, FileMode.Open);
int bufferSize = 1 * 1024 * 1024;
byte[] buffer = new byte[bufferSize];
int len;
long position = fs.Position;
long totalWrittenBytes = 0;
HttpWebRequest uploadRequest = null;
Stream putStream = null;
try
{
while ((len = fs.Read(buffer, 0, buffer.Length)) > 0)
{

uploadRequest = (HttpWebRequest)WebRequest.Create("http://myuploadUrl");
uploadRequest.Method = "PUT";
uploadRequest.AddRange(position, position + len - 1);
uploadRequest.ContentLength = len;
putStream = uploadRequest.GetRequestStream();

int posi = 0;
int bytesLeft = len;
int chunkSize;
while (bytesLeft > 0)
{
chunkSize = Math.Min(25 * 1024, bytesLeft); //25KB
//HERE IS the WriteAsync part that is being cancelled
await putStream.WriteAsync(buffer, posi, chunkSize, ct);
bytesLeft -= chunkSize;
posi += chunkSize;
totalWrittenBytes += chunkSize;
if (progress != null)
progress.Report((int)(totalWrittenBytes * 100 / fs.Length));
}

putStream.Close();
putStream = null;
position = fs.Position;
var putResponse = (HttpWebResponse)uploadRequest.GetResponse();
putResponse.Close();
uploadRequest = null;
}

//putStream.Write(buffer, 0, len);

}
catch (OperationCanceledException ex)
{
if (putStream != null)
{
putStream.Flush();
putStream.Close();//WebException occur here: Cannot close stream until all bytes are written.
}
return false;
}
finally{
fs.Close();
}
return true;
}

最佳答案

我终于捕获了 WebException 因为 post 请求必须写入预期的字节数(ContentLength 属性)

这是我最后的收获

        catch (OperationCanceledException ex)
{
try
{
if (putStream != null)
putStream.Dispose();
}
catch (WebException) { }
return false;
}
finally{
fs.Close();
}

也许我不确定是否应该尝试处理流?

关于c# - 取消 WriteAsync 操作时为 "Cannot close stream until all bytes are written",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28838057/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com