Hi guys.
I am having problem with using REST and returning response as an XML. I've created basic service from the template and everything looks nice but when I want to serialize my class and return it as a responce the service returns something else.
Take a look:
[WebHelp(Comment = "Sample description for DoWork")]
[WebInvoke(UriTemplate = "DoWork")]
[OperationContract]
public SampleResponseBody DoWork(SampleRequestBody request)
{
//TODO: Change the sample implementation here
return new SampleResponseBody()
{
Value = String.Format("Sample DoWork response: '{0}'", request.Data)
};
}
[WebHelp(Comment = "Returns order state based on client and order number")]
[WebInvoke(UriTemplate = "OrderStatus")]
[OperationContract]
public order_status OrderStatus(q_order_status request)
{
return new order_status()
{
error_id = 0,
client_acr = "client",
order_acr = "order"
};
}
The first method is from the template, the second is mine. Returning structures look like this:
public class SampleResponseBody
{
[DataMember]
public string Value { get; set; }
}
public class q_order_status
{
public string client_acr;
public string order_acr;
}
[DataContract]
[XmlSerializerFormat]
public class order_status
{
[XmlAttribute]
public int error_id;
[XmlElement]
public string error_desc;
[XmlElement]
public string order_acr;
[XmlElement]
public string client_acr;
}
Edited:
When I am on the help page of the REST kit, I am getting this as a sample response for both methods which is wrong (I should not get this for the second method):
<SampleResponseBody>
<Value>String content</Value>
</SampleResponseBody>
When I call first method like this:
User-Agent: Fiddler
Host: ipv4.fiddler:4617
Content-Type: text/xml
Content-Length: 63
<SampleRequestBody>
<Data>bla bla</Data>
</SampleRequestBody>
I receive:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/9.0.0.0
Date: Wed, 30 Sep 2009 09:41:20 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: application/xml; charset=utf-8
Content-Length: 141
Connection: Close
<SampleResponseBody xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Value>Sample DoWork response: 'bla bla'</Value></SampleResponseBody>
Whis is ok.
WHen I call second method like this:
User-Agent: Fiddler
Host: ipv4.fiddler:4617
Content-Type: text/xml
Content-Length: 115
<q_order_status>
<client_acr>String content</client_acr>
<order_acr>String content</order_acr>
</q_order_status>
I am getting this:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/9.0.0.0
Date: Wed, 30 Sep 2009 09:44:18 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: application/xml; charset=utf-8
Content-Length: 67
Connection: Close
<order_status xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/>
And it should return a serialized to XML instance of class order_status
What is wrong?
Thanks in advance.
After edit: ok, the problem was that for [OperationContract]
XmlSerializer
wasn't triggered. [XmlSerializerFormat]
must be inserted right after the [OperationContract]
to override default DataContractSerializer
.