IIS Express 8 - 允许的最大长度

时间:2015-06-16 13:26:17

标签: c# asp.net iis visual-studio-2013 iis-express

如果下面的内容太长,我的问题是Visual Studio 2013中的IIS Express 8是否遵循maxAllowedContentLength属性,或者它是否具有阻止大量请求的重写值

在针对Visual Studio 2013调试某些webapi调用时,我收到此错误:

Maximum request length exceeded.

我搜索了互联网,一切似乎都指向这两个配置条目:

IIS 6-: maxRequestLength
IIS 7+: maxAllowedContentLength

为了安全起见,我已将这些配置条目添加到我的web.config中,值为4294967295。当我尝试拨打控制器时,我仍然收到错误。

这让我觉得我向服务器发送了一些巨大的数据,但是小提琴手告诉我这个:

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< 4,294,967,295我觉得这个请求不应该有任何问题。

我想看的下一个地方是我的服务器代码:

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()

2 个答案:

答案 0 :(得分:2)

让我们看一下抛出的代码:

private void ValidateRequestEntityLength()
{
    if (!this._disableMaxRequestLength && (this.Length > this._maxRequestLength))
    {
        if (!(this._context.WorkerRequest is IIS7WorkerRequest))
        {
            this._context.Response.CloseConnectionAfterError();
        }
        throw new HttpException(SR.GetString("Max_request_length_exceeded"), null, 0xbbc);
    }
}

ctor设置了这些选项:

internal HttpBufferlessInputStream(HttpContext context, bool persistEntityBody, bool disableMaxRequestLength)
{
    this._context = context;
    this._persistEntityBody = persistEntityBody;
    this._disableMaxRequestLength = disableMaxRequestLength;
    HttpRuntimeSection httpRuntime = RuntimeConfig.GetConfig(this._context).HttpRuntime;
    this._maxRequestLength = httpRuntime.MaxRequestLengthBytes;
    this._fileThreshold = httpRuntime.RequestLengthDiskThresholdBytes;
    if (this._persistEntityBody)
    {
        this._rawContent = new HttpRawUploadedContent(this._fileThreshold, this._context.Request.ContentLength);
    }
    int contentLength = this._context.Request.ContentLength;
    this._remainingBytes = (contentLength > 0) ? contentLength : 0x7fffffff;
    this._length = contentLength;
}

您可以使用此HttpRequest功能禁用限制:

public Stream GetBufferlessInputStream(bool disableMaxRequestLength)
{
    return this.GetInputStream(false, disableMaxRequestLength);
}

如果您想知道如何计算字节数限制,请反编译httpRuntime.MaxRequestLengthBytes

答案 1 :(得分:0)

只是一个想法,但来自: http://www.smarterasp.net/support/kb/a1544/how-to-resolve-maximum-request-length-exceeded.aspx

&#34; maxAllowedContentLength以字节为单位测量,而maxRequestLength以千字节为单位测量&#34;

检查它们是否正确设置并再试一次