Request.InputStream is empty when service call is made

倾然丶 夕夏残阳落幕 提交于 2019-12-23 15:35:03

问题


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?


回答1:


Found this on the other answers:

Request.InputStream.Position = 0;



回答2:


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



来源:https://stackoverflow.com/questions/18053098/request-inputstream-is-empty-when-service-call-is-made

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!