Bevin
Bevin

Reputation: 972

Request.InputStream is empty when service call is made

I'm working on an ASP.NET 4.5 application and I've encountered a very annoying issue. After migrating to VS2012 we encountered the same issue as here. The given solution worked, however I've now discovered that another issue is occurring. For some reason, the InputStream that contains the body content of the HTTP request is reported to be empty. The Content-Length header claims that there is data present, but I have no way of accessing it.

The odd thing is that the data seems to be present in the workaround module specified in the linked question above, but the stream is replaced by an empty one at some point between the module and the API call. See the following example:

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

Removing the module from the configuration just causes an exception when accessing the stream, as it did before.

Any ideas?

Upvotes: 4

Views: 10895

Answers (3)

sk md
sk md

Reputation: 83

this happened to me when I was trying to read the stream from Request InputStream for the second time. Request.InputStream.Position = 0; solved my issue as mentioned by Sean.

Upvotes: 0

Oleg Oyun
Oleg Oyun

Reputation: 1

I have been around such problems. I solved it, my the code is published here Read Request Body in ASP.NET

Upvotes: 0

Sean
Sean

Reputation: 15144

Found this on the other answers:

Request.InputStream.Position = 0;

Upvotes: 9

Related Questions