问题描述
我正在尝试构建一个 Web 应用程序,该应用程序可以触发后台任务以将 .zip 文件从 FTP 下载到本地存储。
- 框架:ASP.Net-Core 3.1
- 托管:Azure 服务计划 P2V2
- 作业处理程序: Hangfire
- 源文件大小:1GB - 4GB
在本地运行时,我按预期下载文件没有问题。但是在生产中运行,如果任务运行超过 18 分钟,我会遇到以下问题:
Failed
An exception occurred during performance of the job.
System.Net.WebException
The underlying connection was closed: An unexpected error occurred on a receive
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive
at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw(Exception source)
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.CommandStream.InvokeRequestCallback(Object obj)
at System.Net.CommandStream.Abort(Exception e)
at System.Net.CommandStream.CheckContinuePipeline()
at System.Net.FtpWebRequest.DataStreamClosed(CloseExState closeState)
at System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
at System.Net.FtpDataStream.dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.Net.FtpDataStream.Read(Byte[] buffer,Int32 offset,Int32 size)
at dataimportTool.Controllers.MyController.DownloadFile(String fileName) in D:\home\site\repository\MyProject\Controllers\MyController.cs:line 98
执行任务中使用的代码如下:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://address_removed" + fileName);
request.Credentials = new NetworkCredential("username","password");
request.Proxy = null;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
request.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
long fileSize = response.ContentLength;
request = (FtpWebRequest)WebRequest.Create("ftp://address_removed" + fileName);
request.Credentials = new NetworkCredential("username","password");
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;
var filePath = Path.Combine(Directory.GetCurrentDirectory(),@"wwwroot\downloads",fileName);
using (FtpWebResponse responseFileDownload = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = responseFileDownload.GetResponseStream())
using (FileStream writeStream = new FileStream(filePath,FileMode.Create))
{
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer,Length);
long bytes = 0;
while (bytesRead > 0)
{
writeStream.Write(buffer,bytesRead);
bytesRead = responseStream.Read(buffer,Length);
bytes += bytesRead;
}
}
如果有人能帮助解决这个问题,我们将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)