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

StackOverflow https://stackoverflow.com/questions/18256550

Вопрос

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.

Это было полезно?

Решение

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

Другие советы

If this is a WCF service, it uses data contract serializers by default. Try using data contract attributes to decorate your class instead:

[DataContract(Name = "Test", Namespace="http://test.net/")]
public partial class TestType {

Note: you'll also need to add DataMember attributes to each field or property that you want serialized.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top