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
- Subscribe

simple xml-rpc
that looks as simple xml-rpc
WCF is a bit tricky eith xml-rpc endpoints.
Thanks,
Narconon Vistabay
TRUVEO - C#, REST and WCF 3.5
How do you configure the client to access the AOL search service when the client is behind a corporate firewall?
Basically in the WCF/REST code where to you attach the client's "web proxy" credentials so that the external service can be called?
Regards
Re: TRUVEO - C#, REST and WCF 3.5
Just add the credentials to the ChannelFactory.Credentials.
Kenny Wolf has a more detailed post about how to handle the proxy credentials.
- Mark Blomsma
Re: REST vs SOAP
Hi John,
I think SOAP based webservices are far from gone, but SOAP as the magic solution for cross platform communication is losing ground fast. In fact REST may already be wider implemented. Don Box, one of the creators of SOAP doesn't seem to feel that SOAP has too much future. Too many specifications leading to too much complexity have made SOAP less desirable.
Personally I think SOAP will still be the primary choice for when a company builds an intranet/extranet based webservices, but open, Internet, API's will gear towards to REST.
- Mark Blomsma
Part of the appeal of REST
Part of the appeal of REST is its elegant simplicity...it's easy and fast to develop and think of applications in the CRUD envelope. I think SOAP, however, is simply in the trough of the so-called "hype-cycle". Especially in the case of large organizations (as Mark points out), the use of description, discovery, and policy protocols is just too powerful to ignore.
I actually think the most interesting part of the web services stack is the highest orchestration/workflow layer (e.g. BPEL). After all, when was the last time you used only one web service in isolation?
- Abel
Re: Part of the appeal of REST
Hello Abel,
You're right, part of the appeal is the simplicity but then again how hard are webservices? If I build a webservice with ASP.NET then I get my WSDL for free. As long as there is a WSDL then Visual Studio will generate a complete client proxy and I can just program as if the service is a local method that I want to call.
Really the only major are where REST is a lot more simple is when calling the service using an HTTP GET and you can just type the parameters in the URL of your browser. Giving a developer instant access and instant visual and understandable insight in what the service returns.
Now a SOAP based service can still be configured to accept HTTP GET calls, but the default tends to be POST, because it's not possible to pass complex objects as parameters (which would have to be passed as XML). Now with REST this is where JSON comes in. I'd say JSON is really not that much simpler than XML, but a JSON-object allows itself to be part of the URL. Although I have to admit I don't know how a string with a "\" backslash or "/" forwardslash will behave.
- Mark Blomsma
REST vs SOAP
Mark -
There certianly has been alot of REST vs SOAP debate, especially as it relates to security. Is REST positioned to transition out SOAP in .NET 3.5 and WCF?
John
Small improvement number 1.
Let's sort the results using a LINQ query. All you need to do is change the DumpToConsole method to:
private static void DumpToConsole(Video[] videos) { IEnumerable list = from v in videos orderby v.Title select v; foreach (Video video in list) { // each title on new line Console.WriteLine(video.Title); Console.WriteLine( "-> " + video.Url); } }- Mark