Welcome to part two of this series on how to implement a visual search service for IE8 using the Windows Communication Foundation (WCF) and other .Net technologies or libraries. The first part described the overall scenario of the service and today I want to go into a little more details with respect to the service itself.

With respect to the service there are some determining factors which need to be taken into account when creating the service contract interface which are:

  • The client is determined (The IE8 Visual Search Component) and is a black box for us
  • The schema of the messages accepted by the client is determined and is POX and is outlined here.
  • The client queries the service using plain parameterizes URLs via HTTP GET requests

This translates straight into the following attributes for our service.

  • We have to create a restful service that returns plain XML (no full blown SOAP Services)
  • Since the scenario implies some sort of contract-first design it is not since there is no metadata that describes the service and its operation and this actually implies that there is no contract (if you want to know a little bit more about contract-first I can recommend this article on theserverside.net)
  • We need full control over the serialization of the resulting XML

No with having this knowledge we can start to create the service.

After creating the service project in Visual Studio 2008 the first thing we do is to create the service interface which is quite simple in the sense that it only defines one single service operation at this time. My service operation is named "Search" (which is kickass creative) and accepts a single string parameter which is the search query.

So in the simplest case our service interface would look like this:

[ServiceContract]
public interface IVideoVisualSearchService
{
   [OperationContract]
   SearchItem
Search(string query);
}

However since we do have the attributes mentioned above we need to add some specific attribute.

  1. First is the [WebGet] attribute in order to make the service accessible via HTTP GET requests.
  2. We need to have full control over the structure of the resulting XML and therefore cannot use the DataContractSerializer which WCF uses by default to serialize the responses but need to specify the standard XMLSerializer in the .Net Framework. Therefore WCF introduces an XmlSerializerFormat attribute within the ServiceModel namespace which does exactly this.
  3. In addition we need to configure the service so that it exposes itself as a restful service. Therefore we could use a ServiceFactory in order to skip the sometimes cumbersome configuration steps however for the sake of transparency I create a manual configuration and then we need to add an endpoint with an webHttpBinding.

Having done all those modifications our service contract looks like this:

[ServiceContract]
[XmlSerializerFormat]
public interface IVideoVisualSearchService
{
   [OperationContract]
   [WebGet]
   [XmlSerializerFormat]
   SearchItem Search(string query);
}

and the minimal endpoint configuration in the looks like this:

<endpoint address="" 
             
behaviorConfiguration="myRestfulBehaviour"
             
binding="webHttpBinding"
             
contract="VideoVSearchAggregator.IVideoVisualSearchService" />

and the behavior configuration section is as well minimalistic and looks like this:

<endpointBehaviors>
   <
behavior name="myRestfulBehaviour">
      <
webHttp />
   </
behavior>
</
endpointBehaviors>

This also already marks the end of part II. In the next part I will give some more detailed information about the object model of the service which defines the content of the search results and how it is attributed to generate exactly the XML that the visual search component of IE8 expects. As a small teaser here is already the class class diagram of the object model to construct the response messages.

MessageClassDiagram

Reference:

DeliciousBookmark this on Delicious Share