Are there plans to port the HttpContentExtensions from WCF REST Starter Kit Preview 2?

Topics: Web Api
Apr 13, 2011 at 7:57 PM

Since Microsoft.Http.dll has been superceded by Microsoft.Net.Http.dll, will the corresponding extension methods also be ported?

Thanks.

Apr 13, 2011 at 9:40 PM
Edited Apr 13, 2011 at 9:42 PM

For clarification, I was specifically after ReadAsJsonDataContract<T>().  I used Reflector to write it myself.  I ended up with the following code:

public static class HttpContentExtensions
{
	public static T ReadAsJsonDataContract<T>(this HttpContent content)
	{
		return (T)content.ReadAsJsonDataContract(new DataContractJsonSerializer(typeof(T)));
	}

	public static object ReadAsJsonDataContract(this HttpContent content, DataContractJsonSerializer serializer)
	{
		return serializer.ReadObject(content.ContentReadStream);
	}
}

This was not a straight copy/paste from Reflector.  It appears that ReadAsStream() has also been deprecated in favor of ContentReadStream.  I'm not sure that I am correctly inferring that but my code does compile and work.This begs the question, has ReadAsJsonDataContract<T>() been deprecated or simply not ported yet?  I'm leaning towards using James Newton-King's Json.NET instead of going down this path as that code seems to be widely accepted as the definitive library.

Apr 14, 2011 at 2:45 AM

Yes there are new ReadAs<T> extension methods that will replace this functionality.

Apr 14, 2011 at 6:55 PM
DarrelMiller wrote:

Yes there are new ReadAs<T> extension methods that will replace this functionality.


Thanks for the quick response.  How would that work for the above example?  Wouldn't it really need to be something like ReadAs<TInput, TSerializer>?

Apr 14, 2011 at 7:31 PM

If I remember correctly one of the overloads allows you to pass in a collection of Formatters that will handle the deserialization.  The appropriate formatter will be selected based on the media type of the HttpContent.

Apr 14, 2011 at 8:03 PM
DarrelMiller wrote:

If I remember correctly one of the overloads allows you to pass in a collection of Formatters that will handle the deserialization.  The appropriate formatter will be selected based on the media type of the HttpContent.


Gotcha.  Thank you.  Can these be expected in Preview 4?

Apr 14, 2011 at 8:31 PM

I'm hoping sometime today!

Apr 15, 2011 at 2:41 AM

I was able to port:

	return httpResponseMessage.Content.ReadAsJsonDataContract<User>();

to:

	return httpResponseMessage.Content.ReadAs<User>(new[] { new JsonMediaTypeFormatter() });

and get it working.  However, I initially tried:

	return httpResponseMessage.Content.ReadAs<User>(new[] { new JsonValueMediaTypeFormatter() });

with no success.  I get the following exception:

	No 'MediaTypeFormatter' is available to read an object of type 'User' with the media type 'application/json'.

I'm unclear why JsonValueMediaTypeFormatter fails and I would expect it to be preferable to JsonMediaTypeFormatter since my service's return type is JsonValue.  In looking at the source for JsonValueMediaTypeFormatter, "application/json" is clearly added as a supported type.

Any ideas?

Coordinator
Apr 15, 2011 at 2:46 AM
You have to use JsonValue with that formatter. Not a real type.

Sent from my Windows Phone

From: davidpeden3
Sent: Thursday, April 14, 2011 7:41 PM
To: Glenn Block
Subject: Re: Are there plans to port the HttpContentExtensions from WCF REST Starter Kit Preview 2? [wcf:253778]

From: davidpeden3

I was able to port:

	return httpResponseMessage.Content.ReadAsJsonDataContract<User>();

to:

	return httpResponseMessage.Content.ReadAs<User>(new[] { new JsonMediaTypeFormatter() });

and get it working. However, I initially tried:

	return httpResponseMessage.Content.ReadAs<User>(new[] { new JsonValueMediaTypeFormatter() });

with no success. I get the following exception:

	No 'MediaTypeFormatter' is available to read an object of type 'User' with the media type 'application/json'.

I'm unclear why JsonValueMediaTypeFormatter fails and I would expect it to be preferable to JsonMediaTypeFormatter since my service's return type is JsonValue. In looking at the source for JsonValueMediaTypeFormatter, "application/json" is clearly added as a supported type.

Any ideas?

Apr 15, 2011 at 2:50 AM
gblock wrote:
You have to use JsonValue with that formatter. Not a real type.

Sent from my Windows Phone

 Got it.  Thank you for the clarification and insta-response!

Apr 15, 2011 at 2:55 AM

One last question on this topic:

I'm using HttpClient to test the service and had to add an Accept header:

	httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

to get it to work.  Without this, it tries to serialize the response as XML which obviously fails despite the fact that my service's return type is JsonValue.

Is this working as intended?