Mark
Mark

Reputation: 53

WCF service support both data contract serializer and xml serializer

I would like to create a WCF Service that would return both Json and XML based on the "Accept" header in the request. I would like to use the XML serializer because I have more control over the xml output. For returning json I need to use the data contract serializer. I see that it is not possible to configure both. Does anyone have any ideas on how I can implement this?

Thanks

Upvotes: 0

Views: 576

Answers (1)

Kevin Junghans
Kevin Junghans

Reputation: 17540

I assume from your question that you want to create a REST type API that returns XML or JSON based on the header of the request, not an API that uses SOAP. I am certain that this is possible in WCF but you have to get the configuration just right and I am having trouble finding a good source of the proper configuration. There are clues all over the web (like here) but not one source. I thought I had an example running but it appears some recent changes I made broke it.

REST in WCF is too hard. My recommendation is to use the ASP.NET Web API to build REST services. It part of MVC 4.0 and it is easy to use and configure. Right out of the box it works like you would expect a REST API to work and will return JSON or XML dependent upon the setting for "Accept" in the header. Note that the history of this Microsoft product is that it started out as the WCF REST Start Kit, which then became WCF Web API, and is now finally ASP.NET Web API. If you use MVC picking this up is a easy.

Upvotes: 2

Related Questions