如何解决将大文件从 FTP 下载到 Azure Web 应用程序时出现 ASP.Net-Core WebException
我正在尝试构建一个 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 举报,一经查实,本站将立刻删除。