Service Description Example:

using System;
using System.ServiceModel;
using FlickrSL.Data;
using SilverlightRestServices.Description;

namespace FlickrSL
{
    [ServiceContract]
    [RestService] // Required to enable the REST extension for your service
    public partial interface Flickr
    {
        [OperationContract(AsyncPattern = true)]
        [RestGet("services/rest/?method=flickr.photos.getInfo&photo_id={0}")] // URI Templates are passed to a simple call to String.Format(template, args...)
        IAsyncResult BeginPhotos_GetPhotoInfo(Int64 photoId, AsyncCallback callback, Object state);

        FlickrResponse<PhotoInfo> EndPhotos_GetPhotoInfo(IAsyncResult result);

        [OperationContract(AsyncPattern = true)]
        [RestGet("services/rest/?method=flickr.photos.getSizes&photo_id={0}")]
        IAsyncResult BeginPhotos_GetSizes(Int64 photoId, AsyncCallback callback, Object state);

        FlickrResponse<SizeCollection> EndPhotos_GetSizes(IAsyncResult result)
    }
}


For Return Types and Body Types you can use either IXmlSerializable, XmlType attributes, or JSON Serialization.

An XmlType Example:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace FlickrSL.Data
{
    [XmlType("photo")]
    public class PhotoInfo
    {
        public PhotoInfo()
        {
            this.Tags = new List<Tag>();
            this.Urls = new List<Url>();
            this.Notes = new List<Note>();
        }

        [XmlAttribute("id")]
        public Int64 Id { get; set; }

        // Truncated for brevity

        [XmlArray("notes")]
        [XmlArrayItem("note")]
        public List<Note> Notes { get; set; }

        [XmlArray("tags")]
        [XmlArrayItem("tag")]
        public List<Tag> Tags { get; set; }

        [XmlArray("urls")]
        [XmlArrayItem("url")]
        public List<Url> Urls { get; set; }
    }

    [XmlType("user")]
    public class OwnerInfo
    {
        [XmlAttribute("nsid")]
        public String UserId { get; set; }

        [XmlAttribute("username")]
        public String Username { get; set; }

        [XmlAttribute("realname")]
        public String RealName { get; set; }

        [XmlAttribute("location")]
        public String Location { get; set; }
    }
}


An IXmlSerializable Example:

using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
using System;

namespace FlickrSL.Data
{
    public class SizeCollection : Dictionary<SizeType, Size>, IXmlSerializable
    {
        static readonly XmlSerializer SizeSerializer = new XmlSerializer(typeof(Size));

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            reader.ReadStartElement("sizes");

            if (!reader.IsEmptyElement)
            {
                reader.MoveToContent();
                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    Size s = (Size)SizeSerializer.Deserialize(reader);
                    this.Add(s.Type, s);

                    reader.MoveToContent();
                }
            }
            else
            {
                reader.Skip();
            }
        }

        public void WriteXml(XmlWriter writer)
        {
            throw new NotSupportedException();
        }
    }
}


Lastly, creating a channel in code:

            ChannelFactory<Flickr> cf = new ChannelFactory<Flickr>(new RestBinding(), new EndpointAddress("http://api.flickr.com/"));
            cf.Open();

            Flickr chan = cf.CreateChannel();

Last edited Jul 18, 2010 at 6:56 AM by sflanker, version 1

Comments

wuffus Jun 3, 2011 at 10:55 AM 
That looks really useful. Maybe you could add a more complete example of how to use this.
How do I create the client object implementing the Flickr interface?