c# - 进行服务调用时 Request.InputStream 为空

标签 c# asp.net iis .net-4.5

我正在开发一个 ASP.NET 4.5 应用程序,我遇到了一个非常烦人的问题。迁移到 VS2012 后,我们遇到了与 here 相同的问题.给定的解决方案有效,但我现在发现另一个问题正在发生。由于某种原因,包含 HTTP 请求正文内容的 InputStream 被报告为空。 Content-Length header 声称存在数据,但我无法访问它。

奇怪的是,数据似乎存在于上面链接问题中指定的解决方法模块中,但在模块和 API 调用之间的某个时刻,流被空流替换。请参阅以下示例:

public class WcfReadEntityBodyModeWorkaroundModule : IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += context_BeginRequest;
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = sender as HttpApplication;
        //This will force the HttpContext.Request.ReadEntityBody to be "Classic" and will ensure compatibility..

        Stream stream = app.Request.InputStream;
        // This stream has data...
    }
} 

...

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml)]
    public Dictionary<string,string> SaveAudioFile()
    {
        Stream s = HttpContext.Current.Request.InputStream;
        // ...but this one does not. Request.ContentLength is nonzero, but
        // the InputStream.Length property is zero.
        ...
    }

从配置中删除模块只会在访问流时导致异常,就像以前一样。

有什么想法吗?

最佳答案

在其他答案上找到这个:

Request.InputStream.Position = 0;

关于c# - 进行服务调用时 Request.InputStream 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18053098/

相关文章:

html - 如何在 web.config 中添加多个 URL 重写规则

c# - 在 IIS 7.5 中重命名默认网站是否安全?

c# - Entity Framework : single database, 多个上下文,正确处理事务

c# - 可以在没有数据库表的情况下生成序列号吗?

.net - 使用 Masterpage 在 ASP.Net 页面上进行 javascript 验证的最佳方法是什么?

asp.net - POODLE漏洞。如果客户端的浏览器可以回退到IIS上的SSL 3.0,则发出警告

c# - 单声道中的 WCF Udp 发现

c# - String.Split 效率问题

c# - 忽略 TcpClient/NetworkStream 中的传入数据

c# - 无法修改数据集