我应该如何使用HttpRequest.GetBufferlessInputStream? [英] How should I use HttpRequest.GetBufferlessInputStream?

查看:226
本文介绍了我应该如何使用HttpRequest.GetBufferlessInputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WCF服务中接收发布数据时遇到问题.如果我尝试使用InputStream,则会出现异常. 在调用HttpRequest.GetBufferlessInputStream之后,不支持此方法或属性"

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

我正在使用.Net 4.5,所以我遵循了帖子中建议的解决方案,但无济于事.尝试访问InputStream时仍然出现错误.

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.

如果我知道如何正确处理无缓冲输入流,那对我来说真的不是问题.当我查看流的长度时,它似乎包含我发送的数据,但是当我使用StreamReader读取它时,它始终不返回任何内容.如果我只调用Stream.Read(),则它总是显示为空.

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?

我都尝试过:

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

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

推荐答案

您可以创建HttpModule(请参见

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>

这篇关于我应该如何使用HttpRequest.GetBufferlessInputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆