Occasionally, an upload of a gzipped file from a phone app to the web service fails with the following error:
Error reading MIME multipart body part.
at System.Net.Http.HttpContentMultipartExtensions.<MultipartReadAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContentMultipartExtensions.<ReadAsMultipartAsync>d__0`1.MoveNext()
The endpoint itself is pretty basic:
[System.Web.Http.HttpPost]
public async Task<HttpResponseMessage> TechAppUploadPhoto()
{
if (!Request.Content.IsMimeMultipartContent())
{
return Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, "The request isn't valid!");
}
try
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (StreamContent file in provider.Contents)
{
Stream dataStream = await file.ReadAsStreamAsync();
String fileName = file.Headers.ContentDisposition.FileName;
fileName = [unique name];
String filePath = Path.Combine(ConfigurationManager.AppSettings["PhotoUploadLocation"], fileName);
using (var fileStream = File.Create(filePath))
{
dataStream.Seek(0, SeekOrigin.Begin);
dataStream.CopyTo(fileStream);
fileStream.Close();
dataStream.Close();
}
// Enable overwriting with ZipArchive
using (ZipArchive archive = ZipFile.OpenRead(filePath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
entry.ExtractToFile(Path.Combine(ConfigurationManager.AppSettings["PhotoUploadLocation"], entry.FullName), true);
}
}
File.Delete(filePath);
}
return Request.CreateResponse(HttpStatusCode.Accepted);
}
catch (Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "PostCatchErr: " + e.Message + e.StackTrace);
}
}
This error appears to be pretty random and hard to recreate. Unfortunately, I didn't write either end and don't have much experience here, but it doesn't appear to be related to the size of the upload either - it's a large limit in the web config and larger files can be uploaded than those that error out. Is there something I'm missing that could cause this issue? It only appears to read the body information once, which is the other possible cause I've found. Any thoughts?