Ugly Stool Rotating Header Image

WCF REST Client for SmugMug – 3 of n

The last post covered my failed attempts (to me) to use an XML schema to generate the XML parser.  Instead of continuing down the path of an auto-generated parser, I chose a slightly different approach.  I pushed all communication activities into WCF until the input was .NET methods, and the output was .NET types.  The following graphic better illustrates how WCF was used.

wcf-post-01-a

The magic of the green WCF strip is done with the IClientMessageFormatter interface.  Obra implements this interface, and specifically overrides the DeserializeReply  method to pre-process SmugMug’s XML responses.  DeserializeReply is used to strip the outer <rsp/> element, and the <method/> element from the XML response.

This XML data…

<rsp stat="ok">
  <method>smugmug.images.get</method>
  <Images>
    <Image FileName="image-1.jpg" Format="JPG" id="1001">
      <Album Key="FxWoe" id="1000"/>
    </Image>
  </Images>
</rsp>

…is converted to this XML data.

<Images>
  <Image FileName="image-1.jpg" Format="JPG" id="1001">
    <Album Key="FxWoe" id="1000"/>
  </Image>
</Images>

The pre-processed XML data contains only the relevant type metadata.  All of the extraneous information has been removed.  Writing a .NET class to represent these data are simple.  A class to represent the <Images/> element is show below.  This class is automatically picked up by the WCF XML deserailizer due to the XmlRoot attribute, and because it is located in the same .NET assembly as the WCF client.

[XmlRoot("Images")]
public class ImagesContract
{
    private readonly List<ImageContract> image =
        new List<ImageContract>();
    [XmlElement("Image")]
    public List<ImageContract> Images
    {
        get { return image; }
    }
}

Transforming the XML data to an instance of a class is done by the default implementation of IClientMessageFormatter.  Obra implements the IClientMessageFormatter interface, but it does so only to pre-process the response.  Once the response is pre-processed, Obra passes the response to the default implementation of DeserializeReply.

Obra’s implementation of DeserializeReply also checks for SmugMug error responses.  An example response is shown below.  If an error is encountered, a exception is throw with the information contained in the response.

<rsp stat="fail">
  <err code="17" msg="invalid method"/>
</rsp>

I have shown a better (at least alternative) solution for consuming a REST (POX) web service using WCF.  If you are parsing XML in your client you can do much better.  WCF exposes hooks that obviate the need for it, and WCF makes it trivial to communicate with a web service in a more type-safe manner.  Error conditions can be caught and dealt with earlier, which should make client code more straight-laced.  WCF makes it easy to push communication code away from your client.

In the next post I will cover how Obra uses WCF to send data to SmugMug.

Comments are closed.

Page optimized by WP Minify WordPress Plugin