gpt4 book ai didi

c# - IIS Express 8 - 最大允许长度

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:53 25 4
gpt4 key购买 nike

万一以下内容太长,我的问题是 Visual Studio 2013 中的 IIS Express 8 是否遵守 maxAllowedContentLength 属性,或者它是否具有一些可防止大请求的覆盖值


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

超过最大请求长度。

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

IIS 6-: maxRequestLength
IIS 7+: maxAllowedContentLength

为了安全起见,我已将这两个配置条目添加到我的 web.config 中,值为 4294967295。当我尝试调用 Controller 时,我仍然收到错误。

这让我觉得我正在向服务器发送大量数据,但是 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 < 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" />

这是完整的异常(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

和内部异常

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

最佳答案

让我们看看抛出的代码:

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

构造函数设置这些选项:

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

关于c# - IIS Express 8 - 最大允许长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30868950/

25 4 0
文章推荐: c# - 将 Winforms ListBox 集合绑定(bind)到 List