Browse Source

#1481: Improve exception handling in case of MEF initialization errors.

pull/1515/head
Siegfried Pammer 6 years ago
parent
commit
286353984e
  1. 29
      ILSpy/App.xaml.cs
  2. 21
      ILSpy/MainWindow.xaml.cs

29
ILSpy/App.xaml.cs

@ -32,6 +32,7 @@ using ICSharpCode.ILSpy.TextView;
using ICSharpCode.ILSpy.Options; using ICSharpCode.ILSpy.Options;
using Microsoft.VisualStudio.Composition; using Microsoft.VisualStudio.Composition;
using System.Text;
namespace ICSharpCode.ILSpy namespace ICSharpCode.ILSpy
{ {
@ -71,7 +72,13 @@ namespace ICSharpCode.ILSpy
} }
} }
InitializeComponent(); InitializeComponent();
if (!System.Diagnostics.Debugger.IsAttached) {
AppDomain.CurrentDomain.UnhandledException += ShowErrorBox;
Dispatcher.CurrentDispatcher.UnhandledException += Dispatcher_UnhandledException;
}
TaskScheduler.UnobservedTaskException += DotNet40_UnobservedTaskException;
// Cannot show MessageBox here, because WPF would crash with a XamlParseException // Cannot show MessageBox here, because WPF would crash with a XamlParseException
// Remember and show exceptions in text output, once MainWindow is properly initialized // Remember and show exceptions in text output, once MainWindow is properly initialized
try { try {
@ -107,16 +114,10 @@ namespace ICSharpCode.ILSpy
// This throws exceptions for composition failures. Alternatively, the configuration's CompositionErrors property // This throws exceptions for composition failures. Alternatively, the configuration's CompositionErrors property
// could be used to log the errors directly. Used at the end so that it does not prevent the export provider setup. // could be used to log the errors directly. Used at the end so that it does not prevent the export provider setup.
config.ThrowOnErrors(); config.ThrowOnErrors();
} } catch (Exception ex) {
catch (Exception ex) {
StartupExceptions.Add(new ExceptionData { Exception = ex }); StartupExceptions.Add(new ExceptionData { Exception = ex });
} }
if (!System.Diagnostics.Debugger.IsAttached) {
AppDomain.CurrentDomain.UnhandledException += ShowErrorBox;
Dispatcher.CurrentDispatcher.UnhandledException += Dispatcher_UnhandledException;
}
TaskScheduler.UnobservedTaskException += DotNet40_UnobservedTaskException;
Languages.Initialize(exportProvider); Languages.Initialize(exportProvider);
EventManager.RegisterClassHandler(typeof(Window), EventManager.RegisterClassHandler(typeof(Window),
@ -124,7 +125,17 @@ namespace ICSharpCode.ILSpy
new RequestNavigateEventHandler(Window_RequestNavigate)); new RequestNavigateEventHandler(Window_RequestNavigate));
ILSpyTraceListener.Install(); ILSpyTraceListener.Install();
} }
protected override void OnStartup(StartupEventArgs e)
{
var output = new StringBuilder();
if (ILSpy.MainWindow.FormatExceptions(StartupExceptions.ToArray(), output)) {
MessageBox.Show(output.ToString(), "Sorry we crashed!");
Environment.Exit(1);
}
base.OnStartup(e);
}
string FullyQualifyPath(string argument) string FullyQualifyPath(string argument)
{ {
// Fully qualify the paths before passing them to another process, // Fully qualify the paths before passing them to another process,

21
ILSpy/MainWindow.xaml.cs

@ -24,6 +24,7 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
@ -451,6 +452,16 @@ namespace ICSharpCode.ILSpy
} }
bool FormatExceptions(App.ExceptionData[] exceptions, ITextOutput output) bool FormatExceptions(App.ExceptionData[] exceptions, ITextOutput output)
{
var stringBuilder = new StringBuilder();
var result = FormatExceptions(exceptions, stringBuilder);
if (result) {
output.Write(stringBuilder.ToString());
}
return result;
}
internal static bool FormatExceptions(App.ExceptionData[] exceptions, StringBuilder output)
{ {
if (exceptions.Length == 0) return false; if (exceptions.Length == 0) return false;
bool first = true; bool first = true;
@ -459,16 +470,16 @@ namespace ICSharpCode.ILSpy
if (first) if (first)
first = false; first = false;
else else
output.WriteLine("-------------------------------------------------"); output.AppendLine("-------------------------------------------------");
output.WriteLine("Error(s) loading plugin: " + item.PluginName); output.AppendLine("Error(s) loading plugin: " + item.PluginName);
if (item.Exception is System.Reflection.ReflectionTypeLoadException) { if (item.Exception is System.Reflection.ReflectionTypeLoadException) {
var e = (System.Reflection.ReflectionTypeLoadException)item.Exception; var e = (System.Reflection.ReflectionTypeLoadException)item.Exception;
foreach (var ex in e.LoaderExceptions) { foreach (var ex in e.LoaderExceptions) {
output.WriteLine(ex.ToString()); output.AppendLine(ex.ToString());
output.WriteLine(); output.AppendLine();
} }
} else } else
output.WriteLine(item.Exception.ToString()); output.AppendLine(item.Exception.ToString());
} }
return true; return true;

Loading…
Cancel
Save