Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Silverlight Exceptions Via the ErrorWindow

0.00/5 (No votes)
9 Jun 2010 1  
The Silverlight navigation app ErrorWindow exposes the last exception thrown, but doesn't show the INNER exceptions. Here's how you can do it.
For the last THREE WEEKS, I've been trying to get a WCF service stood up on a remote secure server, and access it from a Silverlight navigation application on another secure remote server. It's throwing an exception, and referring me to the inner exception. The problem is that this exception is only thrown whn I try to hit the service when it's deployed on a remote server (as opposed to running the service on my development machine), and the inner exception was not available to me. After some LONG pndering, I came up with a solution.

The exception reporting mechanism in Silverlight navigation apps is through a class called ErrorWindow. This class has a single constructor that accepts an Exception parameter. The problem is that it only shows the top-most exception, and doesn't iterate through all inner exceptions. My solution was to overload the constructor, like so:

C#
public ErrorWindow(string message)
{
    InitializeComponent();
    ErrorTextBox.Text = message;
}


and then I wrote this method in a static class:

C#
//--------------------------------------------------------------------------------
public static string GetTotalException(Exception ex)
{
    StringBuilder result = new StringBuilder(ex.Message);
    result.AppendFormat("{0}{1}", Environment.NewLine, ex.StackTrace);
    if (ex.InnerException != null)
    {
        result.AppendFormat("{0}============{0}Inner exception: {1}", 
                            Environment.NewLine, 
                            GetTotalException(ex.InnerException));
    }
    return result.ToString();
}


Finally, I call it something like this:

C#
string errorText = GetTotalException(e);
ErrorWindow window = new ErrorWindow(errorText);


This allowed me to copy/paste the entire exception chain from the displayed ErrorWindow in my silverlight application.

I still haven't found a resolution to connectivity issue, but now I'm better armed to help others to help me.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here