The way this was finally implemented was using the WCF Raw Method. http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx
Keeping the same DTO mentioned in the question, two operational contracts are created.
One for SOAP/XML GetPersonResponse GetPerson(GetPersonRequest request)
and another one for REST/JSON System.IO.Stream GetPerson_Rest(params...)
.
We would go and implement GetPerson()
as required. Then in GetPerson_Rest()
, we would call GetPerson()
afterwhich the returned GetPersonResponse would be converted into Json using JSON.Net library. Finally the method would return a Stream object, as shown in the following snippet:
string jsonResponse = JsonConvert.SerializeObject(response);
// Serialize into stream of bytes
byte[] rawResponse = Encoding.UTF8.GetBytes(jsonResponse);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
// Return json raw response
return new MemoryStream(rawResponse);