ServiceStack使用.NET的XML DataContract序列化器.除了基础.NET框架实现提供的内容之外,它不可定制.
为了支持自定义请求,您可以覆盖默认请求处理.
ServiceStack的Serialization and Deserialization wiki page显示了自定义请求处理的不同方法:
base.RequestBinders.Add(typeof(MyRequest),httpReq => ... requestDto);
跳过自动反序列化并直接从Request InputStream中读取
告诉ServiceStack跳过反序列化并自己处理它,让你的DTO实现IRequiresRequestStream并自己反序列化请求(在你的服务中):
//Request DTO public class Hello : IRequiresRequestStream { /// <summary> /// The raw Http Request Input Stream /// </summary> Stream RequestStream { get; set; } }
覆盖默认的XML Content-Type格式
如果您更喜欢使用其他XML Serializer,则可以在registering your own Custom Media Type之前覆盖ServiceStack中的默认内容类型,例如:
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);