Tuesday, 16 August 2011

Posting JSON to a Restful WCF service

This should be simple, but WCF isn't very forthcoming when it comes to deserialization errors.


To get this working with an out-of-the-box .Net 4 Rest WCF service:

  1. Start Fiddler.
  2. Go to Fiddler's Request Builder, and select the Raw tab.
  3. Establish the POST URI, and the format of the POST data (this will be shown on the service's help page).
  4. Copy, paste and edit the below sample request.
  5. Execute the request.
Points to note:

  1. The POST address must end in a slash: http://localhost:57036/endpoint/.
  2. The Content-Type must be set to application/json.
  3. The Content-Length must be match the size of the body.
  4. The body's format must be the correct JSON representation of the object. If in doubt check the service's help page.

Saturday, 13 August 2011

Julian Treasure: 5 ways to listen better | Video on TED.com

Julian Treasure: 5 ways to listen better | Video on TED.com

Just watched this great video on listening. Julian provides five steps, of which I found the following useful:

1. Practice 3 minutes a day of silence.
2. Listen in a noisy environment - how many different channels of sound can you hear?
5. RASA - receive, appreciate, summarise, ask.

Tuesday, 2 August 2011

Making a Restful WCF service vary its response

A natural fit with the HTTP architecture is to have a WCF service vary the response contents according to the MIME type set in the Accept header by the client.

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:

  1. Given a request for a resource.
  2. When the Accept header is not set, respond with the appropriate default representation (e.g. XML/JSON object).
  3. 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.
  4. 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.