问题是:当使用IHasResponseStatus和公共ResponseStatus ResponseStatus时,如何使用一个命名空间进行响应{get;组; }属性,并在ResponseStatus上删除前缀d2p1. 我为所有Web服务模型使用单个命名空间http://
我为所有Web服务模型使用单个命名空间http://schemas.tagway.com.ua/types;响应看起来很好,除了节点ResponseStatus,因为ServiceStack:它自动为ResponseStatus添加自己的命名空间xmlns:d2p1 =“http://schemas.servicestack.net/types”.
服务模式:
namespace NTPCore.ServiceModel.Operations.Balance { public class Balance { public Auth auth { get; set; } } public class BalanceResponse : IHasResponseStatus { public ResponseStatus ResponseStatus { get; set; } public int balance { get; set; } public int limit { get; set; } } }
项目NTPCore.ServiceModel中的AssemblyInfo.cs:
[assembly: ContractNamespace("http://schemas.tagway.com.ua/types", ClrNamespace = "NTPCore.ServiceModel.Operations.Balance")] [assembly: ContractNamespace("http://schemas.tagway.com.ua/types", ClrNamespace = "ServiceStack.ServiceInterface.ServiceModel")] //may be this not need...experimenting, nothing happance for me
响应示例:
<BalanceResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.tagway.com.ua/types"> <ResponseStatus xmlns:d2p1="http://schemas.servicestack.net/types"> <d2p1:ErrorCode>String</d2p1:ErrorCode> <d2p1:Errors> <d2p1:ResponseError> <d2p1:ErrorCode>String</d2p1:ErrorCode> <d2p1:FieldName>String</d2p1:FieldName> <d2p1:Message>String</d2p1:Message> </d2p1:ResponseError> </d2p1:Errors> <d2p1:Message>String</d2p1:Message> <d2p1:StackTrace>String</d2p1:StackTrace> </ResponseStatus> <balance>0</balance> <limit>0</limit> </BalanceResponse>ServiceStack使用.NET的内置XML DataContractSerializer进行XML序列化.不幸的是,[assembly:ContractNamespace ..]要生效,你需要用[DataContract]和[DataMember]属性来装饰你的DTO.例如:
[DataContract] public class Balance { [DataMember] public Auth auth { get; set; } } [DataContract] public class BalanceResponse : IHasResponseStatus { [DataMember] public ResponseStatus ResponseStatus { get; set; } [DataMember] public int balance { get; set; } [DataMember] public int limit { get; set; } }
这很难看,但这是为漂亮的XML付出的代价,另一个选择是override the built-in XML Content-Type使用你自己的自定义序列化/反序列化例程 – 但这需要更多的工作.