Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling serialization in Web API / APIController

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!

like image 418
Alwyn Avatar asked Jan 03 '13 18:01

Alwyn


People also ask

What is serialization in Web API?

So, serialization converts the object into a shareable format. With serialization, we can transfer objects: Between client and server via REST APIs or GRPC. Over the network for messaging systems like Kafka or RabbitMQ. Through firewalls as JSON or XML strings.

What is ApiController C#?

Web API Controller is similar to ASP.NET MVC controller. It handles incoming HTTP requests and send response back to the caller. Web API controller is a class which can be created under the Controllers folder or any other folder under your project's root folder.

What is ApiController in .NET core?

ApiController attribute The [ApiController] attribute can be applied to a controller class to enable the following opinionated, API-specific behaviors: Attribute routing requirement. Automatic HTTP 400 responses. Binding source parameter inference. Multipart/form-data request inference.


2 Answers

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

like image 111
Youssef Moussaoui Avatar answered Nov 07 '22 05:11

Youssef Moussaoui


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.

like image 39
Matas Vaitkevicius Avatar answered Nov 07 '22 05:11

Matas Vaitkevicius