The ModelBinding stage seems to have consumed the stream already and so you are unable to read the stream again.
I am guessing the 'hotelId' parameter is coming from the Uri. If Yes, you can decorate it with the [FromUri] attribute like the following:
"public void Put([FromUri]int hotelId) { "
This way your request's body is not read by the model binding stage. I have tried your sample method with [FromUri] and it worked fine. Please let us know if you see otherwise.
Thanks,
Kiran Challa
Marked as answer by manfred.steyer on Feb 21, 2012 08:09 PM
This [FromUri] requirement should also go away with the new preview release, as we are working to more aggresively ensure that the content has not been consumed if there's no reason for it to have been.
However, I want to point out this line of code:
var stream = request.Content.ReadAsStreamAsync().Result;
You should never do this in a server app. At best, the call to .Result will be blocking and waste a server thread out of the thread pool waiting for the result; at worst, you could deadlock the hosting process because the blocked thread owns the current
SynchronizationContext.
You should re-write this method to return Task instead of void. If you're using 4.0, then use .ContinueWith; if you're using 4.5, then mark the method as "async" and use "await" to get the resulting stream.
Marked as answer by manfred.steyer on Feb 21, 2012 08:09 PM
manfred.stey...
Member
35 Points
57 Posts
Accessing Payload using a Stream does not work
Feb 20, 2012 01:10 PM|LINK
Hi,
when I try to access the payload directly via a Stream that I got using this.Request the stream seems to be empty.
[1] shows my sample-method; [2] the http-request.
Wishes,
Manfred
[1]
public void Put(int hotelId) {
var request = this.Request; // HACK für BETA1
var stream = request.Content.ReadAsStreamAsync().Result;
using (var r = new StreamReader(stream)) {
Debug.WriteLine(r.ReadToEnd());
}
}
[2]
User-Agent: Fiddler
Host: localhost
Content-Length: 19
Content-Type: text/json
{ "Name": "Max" }
Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: Accessing Payload using a Stream does not work
Feb 20, 2012 06:35 PM|LINK
The ModelBinding stage seems to have consumed the stream already and so you are unable to read the stream again.
I am guessing the 'hotelId' parameter is coming from the Uri. If Yes, you can decorate it with the [FromUri] attribute like the following:
"public void Put([FromUri]int hotelId) { "
This way your request's body is not read by the model binding stage. I have tried your sample method with [FromUri] and it worked fine. Please let us know if you see otherwise.
Kiran Challa
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Accessing Payload using a Stream does not work
Feb 21, 2012 02:00 PM|LINK
This [FromUri] requirement should also go away with the new preview release, as we are working to more aggresively ensure that the content has not been consumed if there's no reason for it to have been.
However, I want to point out this line of code:
var stream = request.Content.ReadAsStreamAsync().Result;
You should never do this in a server app. At best, the call to .Result will be blocking and waste a server thread out of the thread pool waiting for the result; at worst, you could deadlock the hosting process because the blocked thread owns the current SynchronizationContext.
You should re-write this method to return Task instead of void. If you're using 4.0, then use .ContinueWith; if you're using 4.5, then mark the method as "async" and use "await" to get the resulting stream.