Search code examples
namespacesservicestackresponsexml-namespaces

ResponseStatus xmlns d2p1


The question is: how to use one namespace for response, when using IHasResponseStatus and public ResponseStatus ResponseStatus { get; set; } property, and remove the prefix d2p1 on ResponseStatus.

I use a single namespace http://schemas.tagway.com.ua/types for all web service models; the response looks great except the node ResponseStatus, because ServiceStack: it automatically adds its own namespace xmlns:d2p1="http://schemas.servicestack.net/types" for ResponseStatus.

Service model:

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

AssemblyInfo.cs in project NTPCore.ServiceModel:

[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

Example response:

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

Solution

  • ServiceStack makes use of .NET's built-in XML DataContractSerializer for its XML Serialization. Unfortunately for the [assembly: ContractNamespace ..] to have an effect you need to decorate your DTOs with [DataContract] and [DataMember] attributes. e.g:

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

    It's ugly, but that's the price to pay for pretty XML, the other option is to override the built-in XML Content-Type with your own custom Serialization/Deserialization routines - but that requires more work.