如何在服务堆栈中按类型覆盖 XML 序列化格式 [英] How can I override the XML serialization format on a type by type basis in servicestack

查看:38
本文介绍了如何在服务堆栈中按类型覆盖 XML 序列化格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型需要自定义 XML 序列化 &我想用作 requestDto 上的属性的反序列化

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

对于 JSON,我可以使用 JsConfig.SerializeFn,是否有类似的 XML 钩子?

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

推荐答案

ServiceStack 在底层使用 .NET 的 XML DataContract 序列化程序.除了底层 .NET 的框架实现所提供的内容之外,它不可自定义.

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.

为了支持自定义请求,您可以覆盖默认请求处理.ServiceStack 的序列化和反序列化维基页面 展示了不同的方式自定义请求处理:

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:

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

跳过自动反序列化,直接从请求输入流中读取

告诉 ServiceStack 跳过反序列化并通过让您的 DTO 实现 IRequiresRequestStream 并自己反序列化请求(在您的服务中)来自己处理它:

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; }
}

覆盖默认的 XML Content-Type 格式

如果您更喜欢使用不同的 XML 序列化程序,您可以通过 注册您自己的自定义媒体类型,例如:

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);    

这篇关于如何在服务堆栈中按类型覆盖 XML 序列化格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆