WCF Service deployed using DataContractSerilizer - how to use XMLSerializer in DeserializeRequest
-
Wednesday, April 11, 2012 9:10 PM
I have a WCF service deployed using the DataContract Serializer. Clients can send request in Json or XML. I created a IDispatchMessageFormatter. I would like to use the XMLSerializer when serializing request that are "application/xml". Otherwise I would use the data contract serializer to handle JSON.
How do I go about implementing the DeserializeRequest method so I can use the XMLSerializer to serialize the data stream into my object? And also parse the HTTP query parameters?
[WebInvoke(Method = "POST", UriTemplate = "customer/{customerId}")]
void CreateCustomer(string customerId, Customer customer); public void DeserializeRequest(System.ServiceModel.Channels.Message message, object[] parameters)
{
// How to implement.
}Error I receive.
Unable to deserialize XML body with root name 'Customer' and root namespace '' using DataContractSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.
Thanks
All Replies
-
Friday, April 13, 2012 9:44 AMModerator
Hi,
Maybe you have used the REST service in your code. And Message in REST service is not recommended. You’d better use a string in the REST service. Then you can invoke DataContractSerializer or XML Serrializer base on the content type to deserialize the string to an object.
And here is a sample about how to WCF Extensibility-Message Formatter.
http://blogs.msdn.com/b/endpoint/archive/2011/05/03/wcf-extensibility-message-formatters.aspx
You can refer to the Message Formatter in the below link.
- Marked As Answer by Richey WuModerator Wednesday, April 25, 2012 1:51 AM

