2

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.

madness800
  • 181
  • 1
  • 2
  • 13

2 Answers2

9

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

Community
  • 1
  • 1
skrebbel
  • 9,841
  • 6
  • 35
  • 34
4

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?

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Is this for .Net Framework 4.0? Because I couldn't find BufferedMessageData and BufferedMessage om the System.ServiceModel.Channels namespace – madness800 Jun 16 '10 at 10:07
  • I used it in debugger (watch window) inside of my .Net Framework 4.0 WCF project. – Oleg Jun 16 '10 at 11:40
  • I've even went and add the System.ServiceModel reference into my project and I still couldn't locate the BufferedMessageData object inside System.ServiceModel.Channels. I also tried searching it in Object Browser and no result coming back at all. – madness800 Jun 16 '10 at 12:03
  • You should search in the part "non-public members" in the debugger. If you want to write a code to access the data you should get `System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage` as a instance of `System.ServiceModel.Channels.Message` type and then access some "non-public members" with the way described in http://stackoverflow.com/questions/1850293/wcf-rest-where-is-the-request-data. – Oleg Jun 16 '10 at 12:58
  • `BufferedMessageData` is inaccessible due to its protection level – Nitin Sawant Mar 28 '23 at 12:01