2

I have a type that requires custom XML serialization & deserialization that I want to use as a property on my requestDto

For JSON i can use JsConfig.SerializeFn, is there a similar hook for XML?

chrisortman
  • 1,514
  • 15
  • 22

1 Answers1

5

ServiceStack uses .NET's XML DataContract serializer under the hood. It is not customizable beyond what is offered by the underlying .NET's Framework implementation.

In order to support custom requests you can override the default request handling. ServiceStack's Serialization and Deserialization wiki page shows different ways to customize the request handling:

Register a Custom Request DTO binder

base.RequestBinders.Add(typeof(MyRequest), httpReq => ... requestDto);

Skip auto deserialization and read directly from the Request InputStream

Tell ServiceStack to skip deserialization and handle it yourself by getting your DTO to implement IRequiresRequestStream and deserialize the request yourself (in your service):

//Request DTO
public class Hello : IRequiresRequestStream
{
    /// <summary>
    /// The raw Http Request Input Stream
    /// </summary>
    Stream RequestStream { get; set; }
}

Override the default XML Content-Type format

If you would prefer to use a different XML Serializer, you can override the default content-types in ServiceStack by registering your own Custom Media Type, e.g:

string contentType = "application/xml";
var serialize = (IRequest request, object response, Stream stream) => ...;
var deserialize = (Type type, Stream stream) => ...;

//In AppHost.Configure method pass two delegates for serialization and deserialization
this.ContentTypes.Register(contentType, serialize, deserialize);    
mythz
  • 141,670
  • 29
  • 246
  • 390
  • If I use RequestBinders that will override json serialization too correct? – chrisortman Nov 23 '12 at 21:48
  • It allows you to override all **De-serialization** into the Request DTO for all Content-Types (inc. JSON). If your Request Binder returns null for the Request DTO, than it continues to fallback to using ServiceStack's default Request DTO binding. – mythz Nov 23 '12 at 22:14
  • np - they're the most common options for de-serializing custom requests. More custom hooks and events are described in ServiceStack's [Order of Operations](https://github.com/ServiceStack/ServiceStack/wiki/Order-of-Operations) wiki page. – mythz Nov 24 '12 at 08:40
  • Will this affect the XSD or WSDL generation also? So when they import from add service reference, they will get our custom serializer/deserializer? – iroel Apr 04 '16 at 17:05