Niraj Bhatt – Architect's Blog

Ruminations on .NET, Architecture & Design

Creating HttpContent from a .NET Object – WCF REST Starter Kit

While I was preparing my demo for Tech Ed, I found that it isn’t quite easy to convert .NET Objects to HttpContent (provided with WCF REST Starter Kit) & Vice Versa. Additionally it requires some boilerplate code to be written every time. I thought of abstracting it out in HttpContentHelper class with 2 straight methods – CreateContentFromObject & CreateObjectFromContent. Code is given below:

public static class HttpContentHelper
        public static HttpContent CreateContentFromObject<T>(T obj)
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.Serialize(ms, obj);
            return HttpContent.Create(ms.ToArray(), “application/xml”);

            return HttpContentExtensions.CreateXmlSerializable(obj); //update, see comments below

        public static T CreateObjectFromContent<T>(HttpContent content)
            return content.ReadAsXmlSerializable<T>();

Hope you it find it helpful🙂 .

3 responses to “Creating HttpContent from a .NET Object – WCF REST Starter Kit

  1. ranamauro May 12, 2009 at 12:12 am

    You might want to check out the Microsoft.Http.HttpContentExtensions class in Microsoft.Http.Extensions.dll, it has a bunch of helpers that do exactly what you’re trying to do here.

    Here’s the method that gives you #1:
    public static HttpContent CreateXmlSerializable(T value, params Type[] extraTypes);

    For #2 it seems you already found the System.Xml.Serialization.XmlSerializerContentExtensions class, again, in Microsoft.Http.Extensions.dll. Not sure why you wanted to re-expose it as a static rather than just using the extension method directly, perhaps your feedback can help us improve the API for the next release🙂

  2. nirajrules May 12, 2009 at 1:12 am

    Thanks Ranamauro. My fault🙂 . I didn’t do a thorough check on Extensions dll. But somehow I feel that APIs could have been better named. CreateXmlSerializable & ReadAsXmlSerializable don’t quite fit in (at least initially). May be that could be a reason why I didn’t catch them in first place.

  3. Paulius Raila November 22, 2009 at 10:33 pm

    I stiil think that library has some drawbacks:

    The bad thing is that HttpContentExtensions haven’t worked for me when i needed to add custom prefixes to my
    xml message.:

    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces ();

    Then i’ve just used XmlSerializer to pass this namespaces. haven’t you faced such problem ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: