9

Where can I specify custom serialization/deserialization in an ASP.NET Web API?

The throughput of our application requires a fast serialization/deserialization of messages, hence we need to tightly control this part of the code to either use our home-brew or an OSS one out there.

I have checked various sources such as this that explains how to create a custom value provider, but I have yet to see an example that explains the process end to end.

Can anyone direct/show me the way to serialize the incoming/outgoing messages?

Also a diagram of the various injection points/event sinks in Web API similar to this one for WCF is appreciated!

2
  • Implement ISerializable? Commented Jan 3, 2013 at 18:42
  • Mmm... no that seems like a WCF way of doing things. Commented Jan 3, 2013 at 20:38

2 Answers 2

7

The extension point you're looking for is the MediaTypeFormatter. It controls reading from the request body and writing to the response body. This might be the best resource for writing your own formatter:

http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters

Sign up to request clarification or add additional context in comments.

3 Comments

Looks like this is it, although it seems like there is only a single serializer per host is there a way to have this serializer configured at controller/action level?
It's possible to do. There's a couple ways you can handle this: 1) Either you can explicitly say which formatter you want to use when you create your response. You can use the Request.CreateResponse extension method to pick the formatter you want to use. Or 2) You can use per-controller configuration to customize formatters for a specific controller.
Here's another good doc about extensibility in WebAPI: asp.net/web-api/overview/extensibility/…. In particular, it explains the per-controller configuration I just mentioned.
1

Here's code example in case link in the answer above dies

public class MerlinStringMediaTypeFormatter : MediaTypeFormatter
{
    public MerlinStringMediaTypeFormatter()
    {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
    }

    public override bool CanReadType(Type type)
    {
        return type == typeof (YourObject); //can it deserialize
    }

    public override bool CanWriteType(Type type)
    {
        return type == typeof (YourObject); //can it serialize
    }

    public override Task<object> ReadFromStreamAsync( 
        Type type, 
        Stream readStream, 
        HttpContent content, 
        IFormatterLogger formatterLogger)
    {
        //Here you put deserialization mechanism
        return Task<object>.Factory.StartNew(() => content.ReadAsStringAsync().Result);
    }

    public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
    {
        //Here you would put serialization mechanism
        return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
    }
}

Then you need to register your formatter in Global.asax

protected void Application_Start()
    {
        config.Formatters.Add(new MerlinStringMediaTypeFormatter());
    }

Hope this saves you some time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.