Whilst this is easy to do using the WCF Web API, it is slightly more challenging in an application using regular WCF (even WCF 4).
The specific scenario I needed to handle was:
- Given a request for a resource.
- When the Accept header is not set, respond with the appropriate default representation (e.g. XML/JSON object).
- When the Accept header is set to a relevant MIME type, e.g. application/png, respond with a stream containing the PNG representation of the resource.
- Due to the architecture being used, the method's response type could not be the Message type, but had to be that which represented the default resource representation.
The solution I finally arrived at was greatly added by this great post on IOperationBehaviour and the WCF Web API PngResponseFormatter. A sample solution can be downloaded as a zipped VS 2010 solution.