HttpContext.Current.Request.InputStream property throws exception “This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked.” or HttpContext.Current.Request.Forms parameters empty


In .net 4.5 WCF leveraged the buffer less input stream for scalability benefits. As a result when you try to access the HttpContext.Current.Request.InputStream property you may end up with the below exception, as the InputStream property tries to get you handle to the Classic stream as they both are incompatible. You may also see the other side effect of HttpContext.Current.Request.Form parameters becoming empty.

This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked.”

If you had a WCF 4.0 app which worked perfectly but on upgrading your .net framework to 4.5 you notice the service failing on accessing this property, here is the way to work-around the issue:

  1. Add a simple HttpModule in the same WCF project which will access the InputStream property for each request before WCF reads it so that it will enforce the HttpContext.Request.ReadEntityBody to be “Classic” and will ensure compatibility. 

public class WcfReadEntityBodyModeWorkaroundModule : IHttpModule

    {

        public void Dispose()

        { 

        }

         public void Init(HttpApplication context)

        {

            context.BeginRequest += context_BeginRequest;

        }

         void context_BeginRequest(object sender, EventArgs e)

        {

            //This will force the HttpContext.Request.ReadEntityBody to be “Classic” and will ensure compatibility..

            Stream stream = (sender as HttpApplication).Request.InputStream;

        }

 } 

2. Register this module in your web.config by adding these lines in <configuration><modules> setting.

 <system.webServer>

    <modules …>

      <!– Register the managed module –>

      <add name=”WcfReadEntityBodyModeWorkaroundModule” type=”MyAssembly.WcfReadEntityBodyModeWorkaroundModule, MyAssembly” />

    </modules>

<…

If your project cannot be modified, then you can write this Http module in a separate assembly, GAC it separately and register this module in the web.config.

Now try accessing the service it should succeed for you!

 

Note: This issue has been fixed with .net 4.5.1 update. To get the old behavior (4.0 behavior) back please apply the below compat switch in your web.config:

<configuration>  
  <appSettings>
    <add key=”wcf:serviceHostingEnvironment:useClassicReadEntityBodyMode” value=”true” />
  </appSettings>
</configuration>

Comments (4)

  1. I am having similar problems with WebAPI.  I'm trying to use BufferLess streams to upload files to WebAPI, but when HttpRequest.GetBufferlessInputStream() is called it blows up with he same exception.

    I think I narrowed it down and I'm using WIF with WSFederationmodule.  When this module is in the web config I get the same error.  I tried your solution but it didn't appear to work.

    Any ideas?  I asked this question here as well:  forums.asp.net/…/1

  2. Eliyahu Goldin says:

    Thanks, Praburaj, the module worked for me nicely!

  3. Brian Haynes says:

    Praburaj,

    I tried the appSettings fix with 4.5.1 and I am still getting an empty Request.Forms collection in my global error handler, even though parameters have been passed.  Is there something else I need to do to get the appSettings fix to work?

  4. CB says:

    Thanks. Seemed to have worked. Appreciate it.