Monday, March 07, 2005

Starting with wsdl in mono

Webservices allows us to call remote methods through HTTP, using XML. A web server will export the methdos usind WSDL, so it will be transparent for us.

A simple example could be the Google API, which can be found at http://api.google.com/GoogleSearch.wsdl

You can use wsdl.exe to generate a proxy code to make use of these methods.

Implementing the API:
wsdl http://api.google.com/GoogleSearch.wsdl

Compiling the API:
mcs GoogleSearchService.cs -r:System.Web.Services -target:library

We can build a simple program using the Google API so:
mcs google.cs -r:GoogleSearchService.dll -r:System.Web.Services

Gnome SpellChecker - Proof of concept

Run monodevelop and open a new solution/project. Copy the GoogleSearchService.dll library into your project directory. Go to Solution->References->Edit references->.Net Assembly and select the GoogleSearchService.dll file.

Create a GUI with Glade with: a Label, an Entry and a Button in such a way that when you click the button, our GetSpellingSuggestion(string s) method be called with the s parameter as the Entry.Text content.

private void GetSpellingSuggestion(string s)
{
   try{
        GoogleSearchService svc=new GoogleSearchService();
        string result=svc.doSpellingSuggestion(this.key,s);
        label1.Text="Usted quiso decir: "+result;
   }catch(Exception ex){
        Console.WriteLine(ex.ToString());
   }
}        
The 'key' variable is a string containing the hascode provided by Google, which allows us to access its API. (More information is available at http://www.google.com/apis/)

No comments: