5

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>
pnuts
  • 58,317
  • 11
  • 87
  • 139
Inprimex
  • 147
  • 1
  • 6

2 Answers2

2

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.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thanks for the quick response! But after adding a [DataContract] and [DataMember] situation is the same. I try this: [DataContract(Namespace = "http://schemas.tagway.com.ua/types")] but still nothing change for me... – Inprimex Nov 28 '12 at 00:14
  • oh you can't change ServiceStack's ResponseStatus namespace which is static. If you want pretty XML you'll have to use the default `http://schemas.servicestack.net/types` namespace. – mythz Nov 28 '12 at 00:21
  • mmm, but is it possible to implement my plan about pretty XML, using IService and implementing global error handling? – Inprimex Nov 28 '12 at 00:58
  • These concepts aren't the same. See the [different ways of handling exceptions](http://stackoverflow.com/questions/13058205/serviceexceptionhandler-usage-on-restservicebaset) – mythz Nov 28 '12 at 02:04
  • Think twice and choice case to use http://schemas.servicestack.net/types namespace as more quickly solution. Thank you! – Inprimex Nov 29 '12 at 20:52
0
[CollectionDataContract(Name = "root", ItemName = "row")]
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – Donald Duck Dec 14 '17 at 10:45