进行服务调用时Request.InputStream为空 [英] Request.InputStream is empty when service call is made

查看:467
本文介绍了进行服务调用时Request.InputStream为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

有什么想法吗?

推荐答案

在其他答案上发现了这一点:

Found this on the other answers:

Request.InputStream.Position = 0;

这篇关于进行服务调用时Request.InputStream为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆