我们已经在生产中上传了文件RestApi。内部UploadFile调用另一个API(FileHandlerServlet)将照片保存在物理磁盘上。 昨天,我们的UploadFile REST API收到了大量异常。 异常说明
System.Net.WebException:基础连接已关闭:接收时发生意外错误。 --- System.IO.IOException:无法从传输连接读取数据: 远程主机强行关闭了现有连接。 ---> System.Net.Sockets.SocketException:现有的连接被远程主机强行关闭 在System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小) ---内部异常堆栈跟踪的结尾--- 在System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小) 在System.Net.PooledStream.Read(Byte []缓冲区,Int32偏移量,Int32大小) 在System.Net.Connection.SyncRead(HttpWebRequest请求,布尔userRetrievedStream,布尔probeRead) ---内部异常堆栈跟踪的结尾--- 在System.Net.HttpWebRequest.GetRequestStream(TransportContext&context) 在System.Net.HttpWebRequest.GetRequestStream() 在Photos.WebApis.Utilities.Helper.UploadFilesToRemoteUrl(String url,Byte [] img)在D:\ Source \ Person \ Photos \ Photos.WebApis \ Utilities \ Helper.cs:line 60中 在Photos.WebApis.Controllers.AddPersonalPhotoController.POST(HttpRequestMessage请求,DtoPersonalPhoto照片)位于D:\ Source \ Person \ Photos \ Photos.WebApis \ Controllers \ AddPersonalPhotoController.cs:line 102
乍看之下,当UploadFile API与FileHandlerServlet通信时,似乎存在一些问题。 在这个级别上,我们可以责备FileHandlerServlet或Network。但这种情况并非如此。因为当网络团队重新启动UploadFile API的IIS时,一切正常。 Api是在MVC5,.Net框架4.6.1中开发的。 我的问题“ IIS,应用程序池等是否存在任何问题?”
此问题仅发生过一次。通常,UploadFile API每天处理10K +条记录。
这是示例代码
public string UploadFilesToRemoteUrl(string url, byte[] img)
{
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
request.UseDefaultCredentials = false;
request.PreAuthenticate = true;
var FileServerUserName = WebConfigurationManager.AppSettings["FileUserName"].ToString();
var FileServerPwd = WebConfigurationManager.AppSettings["FilePwd"].ToString();
request.Credentials = new NetworkCredential(FileServerUserName, FileServerPwd);
var boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
var endBoundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--");
string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
string headerTemplate =
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n";
Stream memStream = new MemoryStream();
memStream.Write(boundarybytes, 0, boundarybytes.Length);
var header = string.Format(headerTemplate, "uplTheFile", "MyyyPic");
var headerbytes = Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
memStream.Write(img, 0, img.Length);
memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
request.ContentLength = memStream.Length;
using (Stream requestStream = request.GetRequestStream())
{
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
}
using (var response = request.GetResponse())
{
Stream stream2 = response.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
return reader2.ReadToEnd();
}
}