Part 5: Coping with Trouble - In Part 5 of this series we'll work with the application from Part 4 to add error handling.
Tip For best results, code along with the step-by-step video.
When stuff happens one of the first things you should do is look at the DevForce DebugLog. DevForce always generates tracing messages as it runs, and by default writes those messages on the EntityServer to a file named DebugLog.xml. When a problem occurs in your application, the messages in the log can often help to diagnose the problem.
As shown during the video, if we introduce an error (here we modified the database connection string) a message will be recorded in the log file indicating the problem:
Every application should have error handling, and we've been remiss in not adding it earlier. When using asynchronous queries and saves we can't add a try/catch to handle the problem, as we would with synchronous operations. Instead the exception is returned when the asynchronous operation completes in the Error property.
C# | query.ExecuteAsync( op => { if (op.HasError) { throw new InvalidOperationException("Query failed: " + op.Error); } op.Results.ForEach(Employees.Add); CurrentEmployee = Employees.FirstOrDefault(); }); |
VB | query.ExecuteAsync(Sub(op) If op.HasError Then Throw New InvalidOperationException("Query failed: " & op.Error) End If op.Results.ForEach(Employees.Add) CurrentEmployee = Employees.FirstOrDefault() End Sub) |
To keep your application from terminating due to the exception you can call op.MarkErrorAsHandled().
Here are some links for more detail on what we've covered here, and additional information we hope you find useful.
The user interface for the application built during this tour uses a DataForm component supplied by the Silverlight 4 Toolkit (different from the Silverlight 4 Tools!). You can download the Toolkit here:
http://silverlight.codeplex.com/
To work with this application, as all DevForce 2010 Silverlight applications, you will need Visual Studio 2010, the Silverlight 4 runtime (SilverlightTools.exe), and DevForce installed.