Search code examples
c#.netwcfxml-serializationdatacontract

.Net Rest Web Service response has default Data Contract namespace rather than expected namespace


Details:

Interface Contract:

[OperationContract]
[WebGet(UriTemplate = "test")]
TestType TestOperation();

Type Definition:

[System.Xml.Serialization.XmlRoot(ElementName = "Test", Namespace="http://test.net/", IsNullable=false)]
public partial class TestType {

Actual Result:

<TestType xmlns=http://schemas.datacontract.org/2004/07/ …

Expected Result:

<Test xmlns= http://test.net/ …

Please advise.


Solution

  • The service is using the DataContractSerializer to serialize the response and therefore requires the data contract namespace. In order to override that I would recommend applying the XmlSerialzeFormat Attribute to the operation as follows...

    [OperationContract]
    [WebGet(UriTemplate = "test")]
    [XmlSerializerFormat]
    TestType TestOperation();