我遵循代码来限制应用程序的文件下载速度;
context.Response.Buffer = false;
context.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + arquivo.Nome);
context.Response.AppendHeader("Content-Type",
"application/octet-stream");
context.Response.AppendHeader("Content-Length",
arquivo.Tamanho.ToString());
int offset = 0;
byte[] buffer = new byte[currentRange.OptimalDownloadRate];
while (context.Response.IsClientConnected && offset < arquivo.Tamanho)
{
DateTime start = DateTime.Now;
int readCount = arquivo.GetBytes(buffer, offset, // == .ExecuteReader()
(int)Math.Min(arquivo.Tamanho - offset, buffer.Length));
context.Response.OutputStream.Write(buffer, 0, readCount);
offset += readCount;
CacheManager.Hit(jobId, fileId.ToString(), readCount, buffer.Length, null);
TimeSpan elapsed = DateTime.Now - start;
if (elapsed.TotalMilliseconds < 1000)
{
Thread.Sleep(1000 - (int)elapsed.TotalMilliseconds);
}
}
与往常一样,它适用于我的开发,内部和客户QA环境,但它在生产环境中引发了异常:
System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)
at (...).Handlers.DownloadHandler.processDownload(HttpContext context, ...)
对于用户,将在下载对话框中打开一个新窗口:
The connection with the server was reset
你知道发生了什么吗?
答案 0 :(得分:1)
可能是这个运行的IIS假定您的Web应用程序挂起,因为它的线程在休眠时无法访问。然后它回收工作线程。
您应该尝试简单地减少睡眠间隔。 1秒似乎很高......
答案 1 :(得分:1)
问题是请求运行超过90秒。
我将改变该HTTP处理程序以实现IHttpAsyncHandler
并创建一个后台非阻塞线程。现在一切正常。