Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 263
Bevin Avatar asked Jan 13 '23 17:01

Bevin


1 Answers

Found this on the other answers:

Request.InputStream.Position = 0;
like image 150
Sean Avatar answered Jan 27 '23 18:01

Sean