在 REST WCF 中读取 HttpRequest 正文
我在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更简单,WCF + REST:请求在哪里数据? 工作正常。
另外,如果您的请求正文是可反序列化的,您只需传递一个类即可。除非有一些拼写错误,这应该可以:
...
...
对此资源执行 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:
...
...
Doing POST with data
{"Colour": "blue", "Size": 5}
to this resource should return"It's a blue banana!"
.尝试使用
((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?