在 REST WCF 中读取 HttpRequest 正文

发布于 2024-09-06 11:23:49 字数 476 浏览 10 评论 0原文

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

所以我的问题是,实际上有没有办法访问 WCF 中的 HttpRequest 请求正文?

附: 请求正文中的数据是 JSON 字符串,对于响应,它也会将响应正文中的数据作为 JSON 字符串返回。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难愈 2024-09-13 11:23:49

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

另外,如果您的请求正文是可反序列化的,您只需传递一个类即可。除非有一些拼写错误,这应该可以:

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!";
}

对此资源执行 POST 数据 {"Colour": "blue", "Size": 5} 应该返回 "It's一根蓝色香蕉!”

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!".

万水千山粽是情ミ 2024-09-13 11:23:49

尝试使用 ((System.ServiceModel.Channels.BufferedMessageData)(((System.ServiceModel.Channels.BufferedMessage)((OperationContext.Current.RequestContext).RequestMessage)).MessageData)).Buffer

它有输入 System.ArraySegment

或读取 WCF + REST:请求数据在哪里?

Try with ((System.ServiceModel.Channels.BufferedMessageData)(((System.ServiceModel.Channels.BufferedMessage)((OperationContext.Current.RequestContext).RequestMessage)).MessageData)).Buffer

it has type System.ArraySegment<byte>

or read WCF + REST: Where is the request data?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文