[Request.InputStream进行服务调用时为空

问题描述 投票:3回答:3

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

奇怪的是,数据似乎存在于上面链接的问题中指定的解决方法模块中,但是在模块和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.
        ...
    }

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

有什么想法吗?

c# asp.net iis .net-4.5
3个回答
6
投票

在其他答案上找到了这一点:

Request.InputStream.Position = 0;

0
投票

我一直在解决这样的问题。我解决了,我的代码在这里发布Read Request Body in ASP.NET


0
投票

这是我第二次尝试从Request InputStream读取流时发生的事情。 Request.InputStream.Position = 0;解决了肖恩提到的我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.