在REST WCF中读取HttpRequest Body

mad*_*800 2 json web-services .net-4.0 httprequest wcf-rest

我在.net 4中运行了一个REST WCF服务,我测试了它正在运行的Web服务,并接受我做的HttpRequest.但是我在尝试访问Web服务中的HttpRequest主体时遇到了问题.我已经尝试使用Fiddler和我的WinForm应用程序发送附加在HttpRequest上的随机大小的数据,我似乎无法在运行时找到任何可以找到我的请求主体的对象.我最初的本能是查看HttpContext.Current.Request.InputStream但该属性的长度为0,所以我试着查看IncomingWebRequestContext该对象甚至没有方法也没有属性来获取HttpRequest的主体.

所以我的问题是,有没有办法在WCF中访问HttpRequest请求体?

PS:请求体内的数据是JSON字符串,对于响应,它也会将响应体内的数据作为JSON字符串返回.

skr*_*bel 9

更简单,这个答案在WCF + REST上:请求数据在哪里?工作良好.

此外,如果您的请求正文可反序列化,您只需传递一个类.除非有一些错别字,这应该有效:

public class Banana
{
    public string Colour;
    public int Size;
}
Run Code Online (Sandbox Code Playgroud)

...

[WebInvoke(
    Method = "POST", 
    UriTemplate = "bananas", 
    ResponseFormat=WebMessageFormat.Json, 
    RequestFormat=WebMessageFormat.Json)]
string CreateBanana(Banana banana);
Run Code Online (Sandbox Code Playgroud)

...

public string CreateBanana(Banana banana)
{
    return "It's a " + banana.Colour + " banana!";
}
Run Code Online (Sandbox Code Playgroud)

使用{"Colour": "blue", "Size": 5}此资源的数据执行POST 应该返回"It's a blue banana!".