TRUVEO - C#, REST and WCF 3.5
Last week I blogged about accessing the TRUVEO API from C#. After doing the sample I figured I'd look into doing roughly the same, but now using the Windows Communication Foundation 3.5 (=WCF), which is currently in beta 2 and ships with the .NET Framework 3.5 and Visual Studio 2008.
WCF is slated to have excellent REST support, but resources on the web are rather limited at this time so a little experimentation was in order.
The aim is to use a WCF channel on the clientside to access the TRUVEO XML API on the AOL servers. We'll code the client in C# and the obviously the serverside was not developed using WCF or C#. I don't know what technology the AOL Video Search engine runs on, if you know, please drop me a comment.
Step 1. Provide the WCF client with a contract
WCF operates on service contracts and data contracts. These can be defined by adding attributes interfaces. Since our server technology is unavailable to us we have to derive our contract from the XML that is returned by our service. See code comments for additional information.
First the service contract.
///
/// IAolVideoSearchService defines the services we want to use
/// in our application and which are available on the
/// TRUVEO API.
///
/// For this sample we're only looking at using the Search method
/// of the API.
///
[ServiceContract]
public interface IAolVideoSearchService
{
///
/// By adding the WebGetAttribute we define this service
/// as a REST based service which is accessible using
/// a basic HTTP GET operation.
///
/// The OperationContract marks this operation as part of the WCF
/// service.
///
[WebGet]
[OperationContract]
Response Search(string appid, string method, string query);
}
Next the data contract.
///
/// We need to define the XML schema of the data returned by the
/// Search service. The root is 'Response' and it is defined
/// within the 'http://xml.searchvideo.com' namespace.
///
/// In the sample we define a very minor subset of the XML
/// we expect. Any undefined XML will be discarded by WCF.
///
[DataContract(Namespace = "http://xml.searchvideo.com")]
public class Response
{
///
/// Within the response we find the VideoSet tag, which
/// contains an array of Videos.
///
[DataMember(Name = "VideoSet")]
public Video[] VideoSet;
}
///
/// Video is defined within the 'http://xml.searchvideo.com' namespace.
/// In this sample we only define a video as having a title and a url.
///
[DataContract(Namespace="http://xml.searchvideo.com")]
public class Video
{
[DataMember(Name="title")]
public string Title;
[DataMember(Name = "videoUrl")]
public string Url;
}
Step 2. Use the contract to create a channel
Using the contract we created we can now setup a channel using the WCF factory classes and a WebHttBinding. We'll call the AOL Video Search service and look for Unicef related videos.
namespace DevelopOne.AolWcfSample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("AolWcfSample v1.0 - Using WCF 3.5");
WebHttpBinding webBinding = new WebHttpBinding();
ChannelFactory factory = new ChannelFactory(webBinding, new EndpointAddress("http://xml.searchvideo.com/apiv3"));
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
IAolVideoSearchService proxy = factory.CreateChannel();
Response returnValue = proxy.Search("1x1jhj64466mi12ia", "truveo.videos.getVideos", "Unicef");
DumpToConsole(returnValue.VideoSet);
Console.WriteLine("Done.");
Console.ReadLine();
}
private static void DumpToConsole(Video[] videos)
{
foreach (Video video in videos)
{
// each title on new line
Console.WriteLine(video.Title);
Console.WriteLine( "-> " + video.Url);
}
}
}
}
Step 3. Display the results
Since I created a console application the results of the search displayed as output to the
console. Both the title and url are displayed by the DumpToConsole method.
Step 4. Run the application
Start the console application and view the output.
AolWcfSample v1.0 - Using WCF 3.5 Rock for Kampuchea: Unicef's Rock for Kampuchea -> http://xml.searchvideo.com/rd?i=4104857508&a=70a7dc249f1af3e321b3e0e9402c6b65&p=2 Manu Gin¢bili, embajador de UNICEF -> http://xml.searchvideo.com/rd?i=1891565919&a=70a7dc249f1af3e321b3e0e9402c6b65&p=3 Ronaldinho Gaucho Institute -> http://xml.searchvideo.com/rd?i=4156727172&a=70a7dc249f1af3e321b3e0e9402c6b65&p=4 Female Circumcision Still Rife -> http://xml.searchvideo.com/rd?i=3580935985&a=70a7dc249f1af3e321b3e0e9402c6b65&p=5 Shakira combats violence with UNICEF -> http://xml.searchvideo.com/rd?i=103257968&a=70a7dc249f1af3e321b3e0e9402c6b65&p=6 Portia de Rossi -> http://xml.searchvideo.com/rd?i=2399947612&a=70a7dc249f1af3e321b3e0e9402c6b65&p=7 Tennis Star Federer Visits Tsunami Survivors -> http://xml.searchvideo.com/rd?i=253964641&a=70a7dc249f1af3e321b3e0e9402c6b65&p=8 Angelique Kidjo In The Caf‚ -> http://xml.searchvideo.com/rd?i=74884619&a=70a7dc249f1af3e321b3e0e9402c6b65&p=9 McGregor finishes epic trek -> http://xml.searchvideo.com/rd?i=2199595499&a=70a7dc249f1af3e321b3e0e9402c6b65&p=10 Done.
Step 5. Smile!
Step 6. Improve
Personally I'm not too thrilled about having to pass the appid and methodname in each service call. I'd rather make it a default setting for the Search service. I haven't figured out how to do this, or whether it is even possible.
If you look at this code and know of ways to improve it, let me know!
- Mark Blomsma
[2007-08-26] Updated: AOL Video Search is now called TRUVEO.
- markdeveloper's blog
- Login or register to post comments
