【发布时间】:2015-06-16 13:26:17
【问题描述】:
如果下面的内容太长,我的问题是 Visual Studio 2013 中的 IIS Express 8 是否遵循 maxAllowedContentLength 属性,或者它是否具有阻止大型请求的某些覆盖值
在针对 Visual Studio 2013 调试一些 webapi 调用时,我收到此错误:
Maximum request length exceeded.
我搜索了互联网,一切似乎都指向这两个配置条目:
IIS 6-: maxRequestLength
IIS 7+: maxAllowedContentLength
为了安全起见,我已将这两个配置条目添加到我的 web.config 中,值为 4294967295。当我尝试调用控制器时,我仍然收到错误消息。
这让我觉得我正在向服务器发送大量数据,但 fiddler 告诉我:
Request Count: 1
Bytes Sent: 4,327,084 (headers:1,026; body:4,326,058)
Bytes Received: 3,808 (headers:434; body:3,374)
看看,4,327,084
我想到的下一个地方是我的服务器代码:
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
var provider = new MultipartFormDataStreamProvider(@"C:\tmp", Int32.MaxValue);
var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t =>
{
if (t.IsFaulted || t.IsCanceled)
throw t.Exception; // <== This gets thrown
同样,请求的大小明显小于数据流提供者的缓冲区大小。
总之,我不知道发生了什么。这个请求相当大,但据我所知,这没有理由失败。这是由 VS13/IIS Exp8 引起的吗?还是我还缺少其他东西?
提前感谢您提供的任何帮助。
这是我说我会提供的配置条目
<requestLimits maxAllowedContentLength="4294967295" />
这里是完整的例外
System.IO.IOException: Error reading MIME multipart body part. ---> System.Web.HttpException: Maximum request length exceeded.
at System.Web.HttpBufferlessInputStream.ValidateRequestEntityLength()
at System.Web.HttpBufferlessInputStream.GetPreloadedContent(Byte[] buffer, Int32& offset, Int32& count)
at System.Web.HttpBufferlessInputStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
at System.IO.Stream.<BeginEndReadAsync>b__d(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state)
at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod)
at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at System.Web.Http.WebHost.SeekableBufferedRequestStream.<ReadAsync>d__0.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.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at System.Net.Http.HttpContentMultipartExtensions.<MultipartReadAsync>d__8.MoveNext()
--- End of inner exception stack trace ---
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.Runtime.CompilerServices.TaskAwaiter.GetResult()
at System.Net.Http.HttpContentMultipartExtensions.<ReadAsMultipartAsync>d__0`1.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.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at BICWeb.webservices.controllers.FormsDesignerController.<Post>d__b.MoveNext() in c:\code\BIC_CORE\TRUNK\BIC\src\BICWeb\webservices\controllers\FormsDesignerController.cs:line 300
还有内部异常
System.Web.HttpException (0x80004005): Maximum request length exceeded.
at System.Web.HttpBufferlessInputStream.ValidateRequestEntityLength()
at System.Web.HttpBufferlessInputStream.GetPreloadedContent(Byte[] buffer, Int32& offset, Int32& count)
at System.Web.HttpBufferlessInputStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
at System.IO.Stream.<BeginEndReadAsync>b__d(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state)
at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod)
at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at System.Web.Http.WebHost.SeekableBufferedRequestStream.<ReadAsync>d__0.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.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at System.Net.Http.HttpContentMultipartExtensions.<MultipartReadAsync>d__8.MoveNext()
【问题讨论】:
-
您的数字太大,可能会被视为无效并被忽略。或者,上帝保佑,溢出。将它们设置为 10M,这足以满足此请求。并确保它们以字节为单位,谁知道呢。
-
只是提示,将
throw t.Exception;替换为t.Wait()。这样可以更好地处理错误并且不会破坏存储的堆栈。可能,您无论如何都不应该使用 ContinueWith 并使用 await。 -
@usr 是的,我使用 await 有一段时间了,但我处于“更改所有内容,看看是否有任何工作”模式
-
@usr 至于您的第一条评论,如果您溢出该条目,.Net 实际上会引发配置错误。从 MSDN,我输入的值是允许的最大值,它应该可以工作。
-
其实我忘记了一些重要的事情。发布完整的异常 ToString。这通常是我要求的第一件事,不知道为什么我忘记了。
标签: c# asp.net iis visual-studio-2013 iis-express