Tuesday, February 26, 2008

I've been seeing a shift.    In days gone by the majority of the mobile applications I have produced seem to be mostly disconnected; relying on partial network availability.

So its been a move from SQL Compact merge replication to web-service based solutions.   So as  you all know web-service invocation is a bit of a leap of faith.   Call a webmethod,  add some error handling and hope that you get a response back.     So how can you build fully transactional and reliable mobile applications using this technique?

Here's my 5 point rule of thumb.

1) Test you are actually on the network - I use this

public static bool OnNetwork()
       {
           IPHostEntry mydev = Dns.GetHostEntry(Dns.GetHostName());

           for (int nindex = 0; nindex < mydev.AddressList.Length; nindex++)
           {
               string aux = mydev.AddressList[nindex].ToString();
               if (aux != "127.0.0.1") return true;
           }
           return false;

       }

2) Build a generic handler to you webservice.   Make it easy to change the webmethod url,  I use something like this -

public static gsqa01.RA WS()
   {
       gsqa01.RA ret = new gsqa01.RA();   // our web service
       ret.Url = Core.AppSetting("interneturl"); // pull the url out of a config file
       ret.Credentials = WSCreds();
       ret.PreAuthenticate = true; // save some bandwidth use pre-authenticate
       ret.Proxy = new WebProxy();
       return ret;
   }
   private static System.Net.NetworkCredential WSCreds()
   {
       System.Net.NetworkCredential creds = null;
       if (Settings.User.IndexOf("\\") > -1)   // read our username out of the registry (execuse the shorthand method call)
       {
           string[] parts = Settings.User.Split('\\'); // see if we need to do a domain username split
           string domain = parts[0];
           string user = parts[1];
           creds = new System.Net.NetworkCredential(user, Settings.Password, domain);
       }
       else
       {
           creds = new System.Net.NetworkCredential(Settings.User, Settings.Password);
       }
       return creds;
   }

 

3) Call the webservice method (oh go on),  just use the above like this -

           gsqa01.RA ra = Elements.WS(); // this is the generic bit using the above
           bool loginerror = true;
           try
           {
               lr = ra.Login(user,password); // call the web service

               loginerror=false; // little trick
           }
           catch (Exception e)
           {
            }

4) Make sure when you design a web method the parameters you pass to the webmethod or minimal, optimise for SOAP on low bandwidth i.e keep parameter names nice and short.

5) Check, Check and Check again, your response to make sure you get a correct and validated response.  I normally return a class containing my responses and add some form of flag to indicate the webmethod actually performed the action.

 

I'm probably preaching to the converted.    But by the above five point plan,  I've got fast, reliable and dependable web-service responses working on Windows Mobile.

 

What a hobby...

Tuesday, February 26, 2008 11:55:20 PM UTC  #    Comments [1]  | 

Theme design by Jelle Druyts

Pick a theme: