发布错误:在内部存储之前访问了BinaryRead,Form,Files或InputStream

rid*_*nsb 7 asp.net iis-express asp.net-mvc-4

我的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
Run Code Online (Sandbox Code Playgroud)

完整错误详情:

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)
Run Code Online (Sandbox Code Playgroud)

路线配置:

默认

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" }
    );
}
Run Code Online (Sandbox Code Playgroud)

地区

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+$" }
    );
}
Run Code Online (Sandbox Code Playgroud)

Jim*_*iTh 10

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

错误本身就是试图访问的结果BinaryRead,Form,FilesInputStream以后别的东西已经访问请求实体正文流(换句话说,请求的内容)使用不同的方法.

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

  • BinaryRead(),Form,Files,InputStream(这些都将"合作"与对方),
  • GetBufferedInputStream(), 要么
  • GetBufferlessInputStream()

GetBufferedInputStream 应该与"经典"访问方法配合,根据文档 - 事实上,这是它唯一的区别GetBufferlessInputStream.尽管如此,在这种情况下,错误源于GetBufferedInputStream()使用,其次是InputStream.

的不同方法(不足)的文档中描述ReadEntityBodyMode属性它的枚举类型.

一个最近提交给ASP.NET的WebAPI改变读取体实体的方法,GetBufferedInputStream()它使用前在那里request.InputStream.

这似乎与JsonValueProviderFactory.GetDeserializedObject使用"经典" InputStream属性来读取实体主体的MVC相冲突.如果我们在面值处获取错误消息,GetBufferedInputStream()已经被调用,但是在InputStream访问时没有读取,所以InputStream还没有被填充.

需要做更多测试,但临时修复(由@KiranChalla找到)是在上述提交之前恢复为构建.


dma*_*son 9

由于此问题,我们已恢复使用Web API中的request.InputStream(commit 9c76bb9090490541a453fcead75485ea50b88022).

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();
}
Run Code Online (Sandbox Code Playgroud)

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

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

这里的文件不是特别清楚; 我会问产品团队是否可以改进它.

我们计划在Web API中切换回GetBufferedInputStream; 我们只是确保它在MVC中发挥的作用相对于消耗流和MVC执行而言.


Rob*_*tas 8

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


ody*_*yth 5

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

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

  • 这也是我的问题。为了解决这个问题,我要做的只是在读取`HttpContext.Current.Request.InputStream`之前检查`HttpContext.Current.Request.ContentLength&gt; 0`。 (3认同)