SilverLight App Demonstrating Error Handling

4. May 2009

Read this article in your language IT | EN | DE | ES

Now first thing you should know is this SilverLight app is meant to error and crash.

I was messing around with some Error Handling and just thought I’d post up my demo app. I designed it using Expression Blend 2 SP1 and for SilverLight 2.0

Here’s the logic I wanted to portray…

User attempts to login and on doing so the SilverLight application crashes and they are forced to reload. Forcing the page to reload would typically lose your information. Also, user fails to try and login and we capture those errors and display them on the screen.

Now the output doesn’t look like much, the real value is in the code behind it.

On App Load:

1) App loads and looks for my webservice to provide the BirthYears which binds to the ComboBox. If it cannot find my WebSerivce or the service is down the app captures the error and display a friendly version on the control.

2) I check to see if you have data in the SilverLight Isolated Datastore and if you do I will populate the username into the textbox, simulating the effort of putting things back to the way they were before. (though it might seem like nothing to you, since most login boxes remember your username, this one does not. It’s a coded/manual process)

From my DataStore class which I call, these methods I placed into it

Public Function LoadUser() As User
    Dim User As User = Nothing
    Try
 
        'Instantiate App Store
        Using store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
 
            'If the Store is there
            If store.FileExists(DATA_STORE_NAME) Then
 
                'Lets read it and create a user from the info
                Dim sr As IsolatedStorageFileStream = store.OpenFile(DATA_STORE_NAME, IO.FileMode.Open)
 
                Using reader As New IO.StreamReader(sr)
 
                    Dim strDataLine As String = reader.ReadLine
 
                    If strDataLine IsNot Nothing Then
                        Dim userValues As String() = Split(strDataLine, ";")
                        'UserName.ToString & ";" & Password.ToString & ";" & BirthYear.ToString & ";" & strLastVisit & ";" & IsAuthenticated.ToString
                        Dim BirthYeaer As Integer
                        Integer.TryParse(userValues(2), BirthYeaer)
                        Dim IsAuthenticated As Boolean
                        Boolean.TryParse(userValues(4), IsAuthenticated)
                        User = New User(userValues(0), userValues(1), BirthYeaer, userValues(3), IsAuthenticated)
                    Else
                        User = New User()
                    End If
 
                End Using
 
            Else
                User = New User()
            End If
 
        End Using
    Catch ex As Exception
        Throw ex
    End Try
    Return User
End Function

Scenario 1: Attempt to login with no username or password. App page checks for it and display you a nice message. I’ve sent the error to window as an alert as well as the TextBlock control, just to demonstrate. Easy as pie.

If tbUsername.Text = String.Empty OrElse tbPassword.Password = String.Empty Then
     lblMessage.Text = "You must provide both a Username and a Password"
     HtmlPage.Window.Alert("You must provide both a Username and a Password")
Else
'...
End If

Scenario 2: Attempt to login with any information, doesn’t matter, as long as it’s not blank. This is there the big change comes in. When you try and login as anything but the only user hard-coded into the system. The app throws an error, some critcal error.  You get the alert to then reload the application. At this point in my try…catch I write out data to the SilverLight’s isolated datastore.

Public Sub SaveUser(ByVal User As User)
    Try
 
    
        'Instantiate App Store
        Using store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
 
            Dim sw As IsolatedStorageFileStream = store.OpenFile(DATA_STORE_NAME, IO.FileMode.OpenOrCreate)
 
            Using writer As New IO.StreamWriter(sw)
                writer.WriteLine(User.ToString)
            End Using
 
        End Using
 
    Catch ex As Exception
 
    End Try
End Sub

Unhandled Exceptions: These are a little different. However, in SilverLight you can use the event “Application_UnhandledException” in the App.xaml file and try and come back from an exception. Such as checking to see if you get a communication error and notifying the user, with the ability to cancel the exception and return back to the application.  Most times, we’ll do some cleanup if we can and log the error via a web service to a server to be handled.  The goal here would be to save some information about the application, maybe to have JavaScript fire on the onError event, and try and capture what data it can from the application. Then on reload try and put things back to the way they were before the crash. If the Handled property of ApplicationUnhandledExceptionEventArgs in App.xaml is set to false then JavaScript can capture the error (assuming you set the onError property on the control) and then you get your nice white screen of death. You can excute some js to reload the control.  I didn't demo this here as this plugin is running on my blog and I'd have to toss up some js to capture for it.

A decent book I’d recommend is this one:

 

 

 

Silverlight 2 Unleashed by Laurent Bugnion

Silverlight 2 Recipes: A Problem-Solution Approach by Jit Ghosh, Rob Cameron

Programming, SilverLight, VB.NET , , ,

Comments

5/17/2009 11:42:06 PM #
SilverLight App Demonstrating Error Handling

You are voted (great) - Trackback from Web Development Community

Add comment


(Will show your Gravatar icon)

  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading