Mike Eason
Mike Eason

Reputation: 9713

Catching missing DLL in XamlParseException

I have quite an unusual problem where if there is a missing DLL on the client machine, the application will freeze and display the standard "The application is not responding". However, as I know what the issue is, I'd like to find a way to catch this exception (Missing DLL) and display the message in a dialog displaying meaningful information to help identify which DLL is missing. This will allow the application to have a more graceful death.

Upon debugging on the client machine, I receive the error:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: Could not load file or assembly 'Some.DLL' or one of its dependencies. The system cannot find the file specified.

In release however, the application crashes and is not responding.

Looking at the documentation for this error, a XamlParseException usually occurs inside the InitializeComponent(); method:

For pages of an application, when the XamlParseException is thrown, it is usually in the context of the InitializeComponent call made by your page class, which is the entry point for the WPF application model's usage of the WPF XAML parser at the per-page level. Therefore another possible handling strategy is to place try/catch blocks in InitializeComponent. However, this technique does not integrate well with templates, visual design surfaces and other generated sources that hook up InitializeComponent.

So, I can do something like this:

public MyView()
{
    try
    {  
        InitializeComponent();
    }
    catch (XamlParseException ex)
    { 
        //Do something useful with the error.
    }
}

This is certainly possible, however it would require using this code in practically all controls, which is obviously ridiculous. Not to mention that it doesn't really solve the issue of a missing DLL.

So, my questions are:

Thanks.

Upvotes: 2

Views: 967

Answers (1)

simonalexander2005
simonalexander2005

Reputation: 4577

Yes, it is certainly possible.

To do this, you will need to override what happens when the application first starts up.

Open your Application.xaml.vb, and add the following code:

    protected override void OnStartup(StartupEventArgs e)
    {
        // add an event handler for the UnhandledException event
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(HandleException);
        // start up the application
        base.OnStartup(e);
    }
    // what to do when the exception is thrown
    void HandleException(object sender, UnhandledExceptionEventArgs e)
    {
        // do something with the exception
        MessageBox.Show(e.ExceptionObject.ToString());
    }

The output from the e.ExceptionObject.ToString() contains the problem. In the case you describe, there will probably be nested exceptions, the inner one stating : System.IO.FileNotFoundException: Could not load file or assembly '{missing DLL name here}' or one of its dependencies. The system cannot find the file specified. at {Project}.{where the error was thrown}

Upvotes: 2

Related Questions