本文介绍了阅读的Htt prequest身在REST WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET 4.0中运行的REST WCF服务,我测试过的Web服务,它正在和接受的Htt prequest我做吧。不过,我遇到了一个问题,试图在Web服务访问的Htt prequest体。我试着发送随机大小附加上的Htt prequest同时使用提琴手和我的WinForm的应用程序的数据,我似乎无法找到运行时任何物体在哪里可以找到我的请求主体的位置。我最初的反应是看在 HttpContext.Current.Request.InputStream ,但该属性的长度为0,所以我试图寻找在 IncomingWebRequestContext 的对象甚至没有方法,也没有性能得到Htt的prequest。

所以我的问题是,是否有实际的方式来访问的Htt prequest请求主体的WCF?

PS:请求体中的数据是JSON字符串以及响应它会返回内部响应体的数据作为JSON字符串了。

解决方案

简单得多,在WCF + REST:?在哪里请求数据正常工作

另外,如果你的要求的身体deserializable,你可以通过一个类。除非一些拼写错误,这应该工作:

 公共类香蕉
{
    公共串色;
    公众诠释大小;
}
 

...

  [WebInvoke(
    方法=POST
    UriTemplate =香蕉,
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)
字符串CreateBanana(香蕉香蕉);
 

...

 公共字符串CreateBanana(香蕉香蕉)
{
    返回这是一个+ banana.Colour +香蕉!;
}
 

做POST数据 {颜色:蓝,大小:5} 来此资源应返回这是一个蓝色的香蕉!

I got a REST WCF Service running in .net 4 and I've tested the web service it is working and accepting HttpRequest I make to it. But I ran into a problem trying to access the HttpRequest body within the web service. I've tried sending random sizes of data appended on the HttpRequest using both Fiddler and my WinForm app and I can't seem to find any objects in runtime where I can find my request body is located. My initial instinct was to look in the HttpContext.Current.Request.InputStream but the length of that property is 0, so I tried looking in IncomingWebRequestContext that object doesn't even have a method nor properties to get the body of the HttpRequest.

So my question is, is there actually a way to access the HttpRequest request body in WCF?

PS:The data inside the request body is JSON strings and for response it would return the data inside response body as JSON string too.

解决方案

Much simpler, this answer on WCF + REST: Where is the request data? works fine.

Also, if your request body is deserializable, you can just pass a class. Barring some typos, this should work:

public class Banana
{
    public string Colour;
    public int Size;
}

...

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

...

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

Doing POST with data {"Colour": "blue", "Size": 5} to this resource should return "It's a blue banana!".

这篇关于阅读的Htt prequest身在REST WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:41