This is the seventh post on a series about the new Preview 4 of WCF Web API. The previous posts were:
- Elementary programming model
- Self-hosting, HTTPS and HTTP Basic Authentication
- IIS Hosting
- HTTP Message Classes
- Processing Architecture
This post presents the HTTP content class hierarchy.
The HTTP Message Classes post introduced the HttpRequestMessage and HttpResponseMessage classes, which represent HTTP requests and responses. Both this classes have a Content property, of type HttpContent, that represents the HTTP message body.
The HttpContent abstract class is just the root of a class hierarchy with multiple content classes, as presented in the following class diagram.
I’ve divided this class diagram into several regions, in order to highlight the different design aspects:
- The HttpContent is the hierarchy root, and is referenced by both the HttpRequestMessage and HttpResponseMessage classes.
- There are several concrete HttpContent-derived classes for handling basic content types.
- The StreamContent, ByteArrayContent and StringContent classes are self-explanatory.
- The FormUrlEncoded is used to create application/x-www-form-urlencoded type content, based on a Iterable<KeyValuePair<string,string>> containing (name,value) pairs.
- The ObjectContent class represents object-based content, i. e.
- content produced by the serialization/formatting of an object;
- content that is to be read as an object, using deserialization/unformatting.
- The ObjectContent<T> generic class provides a more typed version of ObjectContent: instead of dealing with plain object, this classes provides methods to obtain a T from the content or to construct a content based on a T.
- This ObjectContent<T> is used by both the new HttpRequestMessage<T> and HttpResponseMessage<T> classes. This two classes represent HTTP request and responses, similarly to the non-generic HttpResponseMessage and HttpRequestMessage classes, with one big difference: the content is strongly-typed – a T instance.
- The conversion between byte streams and object instances is the responsibility of media-type formatters, represented by the MediaTypeFormatter abstract base class. This class contains two abstract methods, OnReadFromStream and OnWriteToStream, implemented by concrete classes such as XmlMediaTypeFormatter or JsonMediaTypeFormatter.
- This post was based on the observation of the Preview 4 source code, available at http://www.codeplex.com. Since this is just a preview, this model is to be interpreted as “work in progress”.
- On future post, I will show how both the content classes and the generic request and response classes can be used as operation parameters.