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!
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With