Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Gain More Performance in ASP Web API, Replace Standard Serialization with MessagePack

0.00/5 (No votes)
25 Jan 2013 1  
Boost up your serialization in ASP.NET Web API.

Introduction

In my previous tip I introduced a new serialization utility called MessagePack. See link. Today I want to show how to replace the default serialization in the ASP.NET Web API.

Background

There are a few pros in replacing the default JSON serializator in Web API  with MessagePack:

  • JSON uses 4 bytes to represent null, MessagePack only requires 1 byte;
  • JSON uses 2 bytes to represent a typical int, MessagePack requires 1 byte and so on;
  • Also, since it’s binary, it’s faster to read and write than JSON.

If you want more info, please consider reading this article link.

Implementation

All this stuff can be achieved by definition using our own MediaTypeFormatter. A media type, also called a MIME type, identifies the format of a piece of data. In HTTP, media types describe the format of the message body. A media type consists of two strings, a type, and a subtype.

When an HTTP message contains an entity-body, the Content-Type header specifies the format of the message body. This tells the receiver how to parse the contents of the message body.

For example, if an HTTP response contains a PNG image, the response might have the following headers:

HTTP/1.1 200 OK
Content-Length: 95267
Content-Type: image/png 

When the client sends a request message, it can include an Accept header. The Accept header tells the server which media type(s) the client wants from the server. For example:

Accept: text/html,application/xhtml+xml,application/xml

This header tells the server that the client wants either HTML, XHTML, or XML.

To create a media formatter, derive from one of these classes:

  • MediaTypeFormatter: This class uses asynchronous read and write methods.
  • BufferedMediaTypeFormatter: This class derives from MediaTypeFormatter but wraps the asynchronous read/write methods inside synchronous methods.

Deriving from BufferedMediaTypeFormatter is simpler, because there is no asynchronous code, but it also means the calling thread can block during I/O.

So, let's implement it, see code below:

public class MediaTypeFormatterCompatibleMessagePack : MediaTypeFormatter
{
    private readonly string _mime = "application/x-msgpack";
    Func<Type, bool> IsAllowedType = (t) =>
        {
            if (!t.IsAbstract && !t.IsInterface && t != null && !t.IsNotPublic)
                return true;
            if (typeof(IEnumerable).IsAssignableFrom(t))
                return true;
            return false;
        }; 
    public MediaTypeFormatterCompatibleMessagePack()
    {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue(_mime));
    }
    {
        if (type == null) throw new ArgumentNullException("type is null");
        return IsAllowedType(type);
    }
    public override bool CanWriteType(Type type)
    {
        if (type == null) throw new ArgumentNullException("Type is null");
                return IsAllowedType(type);
    }
    public override Task WriteToStreamAsync(Type type, object value,
      Stream stream, HttpContent content, TransportContext transportContext)
    {
        if (type == null) throw new ArgumentNullException("type is null");
        if (stream == null) throw new ArgumentNullException("Write stream is null");
        var tcs = new TaskCompletionSource<object>();
        if (type!=typeof(string) && typeof(IEnumerable).IsAssignableFrom(type))
        {
         value = (value as IEnumerable<object>).ToList();
        }
        var serializer = MessagePackSerializer.Create<dynamic>();
        serializer.Pack(stream, value);
        tcs.SetResult(null);
        return tcs.Task;
    }
    public override Task<object> ReadFromStreamAsync(Type type,
      Stream stream, HttpContent content, IFormatterLogger formatterLogger)
    {
        if (content.Headers != null && content.Headers.ContentLength == 0) return null;
        try
        {
            var serializer = MessagePackSerializer.Create(type);
            object result;
            using (var mpUnpacker = Unpacker.Create(stream))
            {
              mpUnpacker.Read();
              result = serializer.UnpackFrom(mpUnpacker);
            }
            tcs.SetResult(result);
        }
        catch (Exception e)
        {
            if (formatterLogger == null) throw;
            formatterLogger.LogError(String.Empty, e.Message);
            tcs.SetResult(GetDefaultValueForType(type));
        }
        return tcs.Task;
    }
}
public override bool CanReadType(Type type)

Client consuming

So let's create a simple entity:

public int PId {get;set;} 
public string Name {get;set;}
public DateTime CreatedAt  {get;set;} 
public string CreatedBy {get;set;} 
public string Description {get;set;} 
}

And a simple sender which uses HttpClient:

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:1212/api/msgPack/1");
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-msgpack"));
public class Person{
var result = client.SendAsync(request).Result;
var serializer = MessagePackSerializer.Create<Models.Person>();

The same but for collections:

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:1212/api/msgPack/");
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-msgpack"));
var result = client.SendAsync(request).Result;
var serializer = MessagePackSerializer.Create<List<WebApi.MessagePack.Models.Url>>();
List<Person> data = serializer.Unpack(result.Content.ReadAsStreamAsync().Result);

And finally posting an example:

var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:49745/api/values/");
request.Content = new ObjectContent<Person>(
    new Person() {
        PId = 1234,
        CreatedAt = DateTime.Now,
        CreatedBy = "Filip",
        Description = "test person" },
    new MessagePackMediaTypeFormatter());
request.Content.Headers.ContentType.MediaType = "application/x-msgpack";
StatusCode result5 = client.SendAsync(request).Result.StatusCode;

Hope you find this interesting for you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here