Controlando la serialización en Web API / APIController
Frecuentes
Visto 6,183 veces
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 este 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 ¡es apreciado!
2 Respuestas
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
Respondido el 03 de enero de 13 a las 18:01
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? - Alwyn
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. - Youssef Moussaoui
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. - Youssef Moussaoui
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());
}
Espero que esto te ahorre algo de tiempo.
Respondido el 01 de Septiembre de 16 a las 14:09
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas asp.net serialization asp.net-web-api deserialization or haz tu propia pregunta.
Implementar
ISerializable
? - Robert HarveyMmm... no that seems like a WCF way of doing things. - Alwyn