Professional, Software

Part 2: Search Application (Live Web Service)

Since Silverlight application only run in sandbox and are not allowed to communicate other than site of origin (you can refer to image/media from anywhere but can not make a web request to anything other than site of origin), I needed to build a webservice that runs from server where my app is deployed from. This itself is pretty easy because of the local web server functionality that VS provides. So to do this….

  1. I created an asmx web service called “LiveWebService”.
  2. Got an APP ID following instructions here.
  3. Added a web reference to http://soap.search.msn.com/webservices.asmx?wsdl where actual web service lives. I named the reference LiveSearch.

LiveWebService.asmx looks like this…

<%@ WebService Language="C#" CodeBehind="~/App_Code/LiveWebService.cs" Class="LiveWebService" %>

LiveWebService.cs looks like this…

 

using System;
using System.Linq;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using LiveSearch;

/// <summary>
/// Summary description for LiveWebService
/// </summary>
[WebService(Namespace = "http://bombayboy5/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class LiveWebService : System.Web.Services.WebService
{
    LiveSearch.MSNSearchService _searchservice;
    public LiveWebService()
    {
        _searchservice = new MSNSearchService();
    }

    [WebMethod]
    public Result[] SearchImages(string searchquery)
    {
        try
        {
            SearchRequest searchRequest = new SearchRequest();
            int arraySize = 1;
            SourceRequest[] sr = new SourceRequest[arraySize];
            sr[0] = new SourceRequest();
            sr[0].Source = SourceType.Image;
            sr[0].ResultFields = ResultFieldMask.All | ResultFieldMask.Image;
            sr[0].Count = 50;
            searchRequest.Query = searchquery;
            searchRequest.Requests = sr;
            searchRequest.AppID = "<Enter your APPID>";
            searchRequest.CultureInfo = "en-US";
            SearchResponse searchResponse;
            searchResponse = _searchservice.Search(searchRequest);
            return searchResponse.Responses[0].Results;

        }
        catch (SoapException fault)
        {
        }
        return null;
    }
}

Now this is pretty straight forward and is a simple wrapper on Live Search API that can return the results back for a given query string. Search Query string is the same string that I get from html page using Scriptable attribute.

Now to use this service…

I added reference to my LiveWebService (created above) in my SearchApplication. Calling it is pretty straight forward. Now my scriptable method that gets called from JavaScript looks something like this…

[Scriptable]
//rememder it should be public
public void Search(string searchstring)
{
    searchresults = null;
    SearchService.LiveWebService livecall = new SearchApplication.SearchService.LiveWebService();
    AsyncCallback searchcompleted = new AsyncCallback(SearchCompleted);
    livecall.BeginSearchImages(searchstring, this.SearchCompleted, livecall);
}


void SearchCompleted(IAsyncResult asyncResult)
{
    SearchService.LiveWebService liveresult = asyncResult.AsyncState as SearchService.LiveWebService;
    searchresults = liveresult.EndSearchImages(asyncResult);
    //add results to UI
}

 

 

I am calling web service asynchronously because I want to show the progress UI while application is waiting for web service to return with the data.

Now my search application gets data from html page and calls a web service that in turn calls real live web service and gets the result back. It still does not have any way of showing these results. Next step will be to show the progress UI while application is waiting for web service to return.

Standard