8

我的 Asp.net MVC 中的任何帖子都会生成此错误:

Server Error in '/' Application.

Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

[InvalidOperationException: Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.]   System.Web.HttpRequest.GetEntireRawContent() +12673515   System.Web.HttpRequest.get_InputStream() +61   System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +132   System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +28   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +248   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +165   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +536   System.Linq.Enumerable.ToList(IEnumerable`1 source) +80   System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +343   System.Web.Mvc.ControllerBase.get_ValueProvider() +57   System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +81   System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +153   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +839919   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag) +27   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +50   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166   System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +827009   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +27   System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +401   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +787114   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +27   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +343   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +12622419   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18213

完整的错误详细信息:

System.InvalidOperationException
Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.

System.InvalidOperationException: Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.
   at System.Web.HttpRequest.GetEntireRawContent()
   at System.Web.HttpRequest.get_InputStream()
   at System.Web.HttpRequestWrapper.get_InputStream()
   at System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext)
   at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
   at Castle.Proxies.Invocations.ValueProviderFactory_GetValueProvider.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Glimpse.Core.Extensibility.CastleInvocationToAlternateMethodContextAdapter.Proceed()
   at Glimpse.Core.Extensions.AlternateMethodContextExtensions.TryProceedWithTimer(IAlternateMethodContext context, TimerResult& timerResult)
   at Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context)
   at Glimpse.Core.Extensibility.AlternateTypeToCastleInterceptorAdapter.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.ValueProviderFactoryProxy.GetValueProvider(ControllerContext controllerContext)
   at System.Web.Mvc.ValueProviderFactoryCollection.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)
   at System.Web.Mvc.ControllerBase.get_ValueProvider()
   at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
   at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state)
   at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
   at System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
   at System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

路线配置:

默认

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "home", action = "index", id = UrlParameter.Optional },
        namespaces: new string[] { "CreditoImobiliarioBB.Web.Controllers" }
    );
}

领域

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Usuario_default"
        ,"usuario/{controller}/{action}/{id}"
        ,new { controller="home", action = "index", id = UrlParameter.Optional }
        , namespaces: new string[] { "CreditoImobiliarioBB.Web.Areas.Usuario.Controllers" }
    );
}

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        name: "Relatorios",
        url: "{controller}/{id}/relatorio/{action}",
        defaults: new { id = 1 },
        constraints: new { id = @"^\d+$" },
        namespaces: new string[] { "CreditoImobiliarioBB.Web.Areas.Relatorios" }
    );
}
public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        name: "Documentos",
        url: "{controller}/{id}/documento/{action}",
        defaults: null,
        constraints: new { id = @"^\d+$" }
    );
}
4

4 回答 4

11

只需在此处发布调查结果 - 作为聊天摘要 - 因为其他人可能会收到此错误。不要发放赏金,因为我实际上没有找到解决方案。;-)

错误本身是由于尝试访问BinaryRead,Form或在Files其他InputStream东西已经使用不同的方法访问请求实体主体流(换句话说,请求的内容)之后的结果。

这些“不同的方法”似乎包括GetBufferedInputStreamGetBufferlessInputStream。这些方法似乎(不确定)都是相互排斥的。换句话说,对于单个请求,您只能使用以下之一:

  • BinaryRead(), Form, Files, InputStream(这些将相互“合作”),
  • GetBufferedInputStream(), 或者
  • GetBufferlessInputStream()

GetBufferedInputStream 根据文档,应该与“经典”访问方法合作- 事实上,这是它与GetBufferlessInputStream. 尽管如此,在这种情况下,错误源于GetBufferedInputStream()使用,然后是InputStream.

ReadEntityBodyMode 属性及其枚举类型的文档中(不充分地)描述了不同的方法。

最近对 ASP.NET WebAPI 的提交将其读取 body 实体的方法更改为GetBufferedInputStream()之前使用request.InputStream.

这似乎与JsonValueProviderFactory.GetDeserializedObject使用“经典”InputStream属性读取实体主体的 MVC 冲突。如果我们从表面上看错误信息,GetBufferedInputStream()它已经被调用了,但是在InputStream被访问时它还没有读完,所以InputStream还没有被填充。

需要进行更多测试,但临时修复(由@KiranChalla 发现)是恢复到上述提交之前的构建。

于 2013-07-16T18:31:34.687 回答
10

由于这个问题(commit 9c76bb9090490541a453fcead75485ea50b88022),我们已经恢复到在 Web API 中使用 request.InputStream 。

JimmiTh 的回答正确地指出了初始发行说明和实现之间的差异。正如上面提到的后面的文档中所暗示的(虽然不是特别清楚),request.GetBufferlessInputStream()、request.GetBufferedInputStream()和request.InputStream(连同Form、Files等)是请求体的三个互斥方法使用权。

看来,与最初的发行说明相反,request.GetBufferedInputStream() 目前不可用,除非同一请求的其他调用者没有使用 InputStream、Form、Files 等中的任何一个。我也在跟进,看看是否有更好的东西可以做到这两种方法的兼容。

大卫

与产品团队交谈后的更新:调用 request.GetBufferedInputStream() 或 request.GetBufferlessInputStream() 意味着“我自己明确地处理流;带我进入禁用自动流处理的高级模式。” 但是,在仅缓冲的情况下,通常依赖于自动流处理的成员(Form、Files、InputStream 等)在缓冲流被完全读取后重新启用。

因此,正如异常消息提示的那样,在完成读取流之前触摸这些属性中的任何一个都将失败:

public ActionResult Index()
{
    Request.GetBufferedInputStream();
    var ignore = Request.InputStream;
    return View();
}

但是在读完流之后这样做会成功:

public ActionResult Index()
{
    var stream = Request.GetBufferedInputStream();
    new StreamReader(stream).ReadToEnd();
    var ignore = Request.InputStream;
    return View();
}

文档在这里不是特别清楚;我会问产品团队是否可以改进它。

我们计划在 Web API 中切换回 GetBufferedInputStream;我们将确保它与 MVC 一起使用时,相对于使用流和 MVC 执行而言,该调用发生的时间更好。

于 2013-07-16T21:27:14.577 回答
8

通过 Web API 上传文件时,我遇到了同样的问题。显然“超出最大请求长度”隐藏在此错误下方,因此在 web.config 中增加maxRequestLength解决了我的情况。

于 2014-10-13T14:48:25.623 回答
6

今天我了解到,如果您使用 WebApi 并且您的流是 aSystem.Net.Http.StreamContent.ReadOnlyStream并且您尝试从中读取 0 个字节 (stream.Read(buffer, 0, 0)),它将抛出 a System.InvalidOperationException,当被 try/catch 捕获时会显示为异常消息“在 HttpRequest.GetBufferedInputStream 的调用者填充内部存储之前访问了 BinaryRead、Form、Files 或 InputStream。”

我意识到这似乎很愚蠢,但是您可以在内存流或文件流上执行相同的操作,并且不会引发异常,在我的情况下,它来自 MsgPackCli,希望这可以节省 4 小时。

于 2015-01-09T09:24:16.033 回答