How should I use HttpRequest.GetBufferlessInputStr

2019-02-26 01:29发布

I have a problem receiving post data in a WCF service. If I try to use the InputStream, I get an exception. "This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked"

I believe I understand why this error is thrown, but I have yet to successfully get around it. There is an existing post on this very issue that points to a few workarounds - Visual Studio 2012 install broke my 2010 WCF project

I am using .Net 4.5, so I followed the solution suggested in the post, but to no avail. I still get an error when attempting to access the InputStream.

It wouldn't really be an issue for me if I knew how to correctly handle the bufferless input stream. When I look at the length of the stream, it seems to contain the data that I send, but when I read it using a StreamReader, it always returns nothing. If I just call Stream.Read(), it always appears to be empty.

My understanding is that Buffered Input was replaced with Bufferless Input for scalability reasons, but it seems some unintuitive. Does anyone know how to properly handle Bufferless Input? Can you provide an example?

I have attempted both:

var result = string.Empty;
 Stream stream = context.Request.GetBufferlessInputStream();
            byte[] b = new byte[100000];
            while ((n = stream.Read(b, 0, b.Length)) > 0)
            {
                result  += n;
            }

and

var stream = context.Request.GetBufferlessInputStream();
var streamContent = new StreamReader(stream).ReadToEnd()

1条回答
祖国的老花朵
2楼-- · 2019-02-26 02:15

You can create an HttpModule (see http://blogs.msdn.com/b/praburaj/archive/2012/09/13/accessing-httpcontext-current-request-inputstream-property-in-aspnetcompatibility-mode-throws-exception-this-method-or-property-is-not-supported-after-httprequest-getbufferlessinputstream-has-been-invoked.aspx) or add a simple configuration parameter if you are using .NET 4.5.1:

<configuration>   
  <appSettings>
    <add key="wcf:serviceHostingEnvironment:useClassicReadEntityBodyMode" value="true" />
  </appSettings>
</configuration>
查看更多
登录 后发表回答