我有一个基于AngularJS的web界面,向我的ApiController
发送一个zip文件。我在那里收到文件:
public async Task<UploadImportPackageResponse> Package()
{
var provider = new MultipartMemoryStreamProvider();
await this.Request.Content.ReadAsMultipartAsync(provider);
var file = provider.Contents.FirstOrDefault(p => p.Headers.ContentDisposition.Name == ""file"");
这在我的开发机器上、在我们的本地测试服务器上运行得很好,但在我们的Azure应用程序服务暂存站点上则不然。
在Azure上,它抛出以下错误:
MIME多部分流意外结束。MIME多部分邮件不完整
System.IO.IOException:
at System.Net.Http.Formatting.Parsers.MimeMultipartBodyPartParser+<ParseBuffer>d__0.MoveNext (System.Net.Http.Formatting, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Net.Http.HttpContentMultipartExtensions+<MultipartReadAsync>d__8.MoveNext (System.Net.Http.Formatting, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Net.Http.HttpContentMultipartExtensions+<ReadAsMultipartAsync>d__0`1.MoveNext (System.Net.Http.Formatting, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at Test.Web.Areas.Admin.Controllers.ImportController+<Package>d__2.MoveNext (Test.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Threading.Tasks.TaskHelpersExtensions+<CastToObject>d__3`1.MoveNext (System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Web.Http.Controllers.ApiControllerActionInvoker+<InvokeActionAsyncCore>d__0.MoveNext (System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Web.Http.Controllers.ActionFilterResult+<ExecuteAsync>d__2.MoveNext (System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Web.Http.Controllers.ExceptionFilterResult+<ExecuteAsync>d__0.MoveNext (System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
这就是我目前所了解到的:
- 1.5 MB的文件可以上传,但12 MB的文件不能上传。(带有
maxRequestLength="256000"
和maxAllowedContentLength="262144000"
) - 我使用Charles Proxy在Azure上捕获了一个导致错误的请求,并在我的本地机器上重新执行,在那里它没有导致错误。所以我相信这是服务器端的问题。不是客户端的问题
我的最佳猜测是服务器缩短了请求时间但我不知道为什么,也不知道要更改什么配置来解决这个问题。
在与Azure支持人员交谈后,他们提到他们已经修复了生产代码中的一个类似错误,所以我查看了所有相关的Microsoft nuget软件包,并意识到MS已经发布了Microsoft.AspNet.WebApi的新版本(以及相关软件包Client、Core、HelpPage和WebHost)
所以Update-Package Microsoft.AspNet.WebApi
对我起了作用。
所以简单的答案是:
请确保已安装5.2.3
或更高版本的Microsoft.AspNet.WebApi