IIS Express 8 - Max allowed length

2019-07-10 03:39发布

In case the below is too long, my question is whether IIS Express 8 in Visual Studio 2013 obeys the maxAllowedContentLength attribute or if it has some overriding value that prevents large requests


When debugging some webapi calls against Visual Studio 2013, I'm receiving this error:

Maximum request length exceeded.

I searched the internet and everything seems to be pointing to these two config entries:

IIS 6-: maxRequestLength
IIS 7+: maxAllowedContentLength

I've added both these config entries to my web.config with a value of 4294967295, just to be safe. When I try to make the call to the controller, I still receive the error.

This made me think that I was sending some gigantic amount of data to the server, but fiddler tells me this:

Request Count:   1
Bytes Sent:      4,327,084      (headers:1,026; body:4,326,058)
Bytes Received:  3,808      (headers:434; body:3,374)

And seeing as how, 4,327,084 < 4,294,967,295 I feel like there shouldn't be any problems with this request.

The next place I thought of looking is my server code:

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

So once again, the size of the request is significantly less than the buffer size for the data stream provider.

So in summary, I have no idea what's going on. The request is fairly big, but from what I can tell, there's no reason this should be failing. Is this caused by VS13/IIS Exp8? Or is there something else I'm missing?

Thanks in advance for any help you can offer.


Here's the config entry I said I would provide

<requestLimits maxAllowedContentLength="4294967295" />

And here's the full exception

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

And the inner exception

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条回答
Root(大扎)
2楼-- · 2019-07-10 04:41

just a thought but from: http://www.smarterasp.net/support/kb/a1544/how-to-resolve-maximum-request-length-exceeded.aspx

"maxAllowedContentLength is measured in bytes while maxRequestLength is measured in kilobytes"

check they're both set correctly and try again

查看更多
Juvenile、少年°
3楼-- · 2019-07-10 04:43

Let's look at the code that throws:

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);
    }
}

The ctor sets those options:

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;
}

And you can use this HttpRequest function to disable the limit:

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

If you want to know how the byte limit is being computed, decompile httpRuntime.MaxRequestLengthBytes.

查看更多
登录 后发表回答