Reputation: 476
I am using a Prism UnityExtensions bootstrapper class to start my WPF application. How do I shutdown the application while the unityextensions bootstrapper is still running?
See my bootstrapper class below. The SomeClass
object may throw a custom exception (fatal). If the custom exception is thrown, I need to close the application. I am using Application.Current.Shutdown()
to shutdown the application.
However, the bootstrapper code continues running, and I get a "ResolutionFailedException was unhandled" exception error when setting the datacontext in the CreateShell()
method. Obviously, the SomeClass
method and interface were not registered with the container due to the catch block.
It appears that the bootstrapper code continues running after a call to Application.Current.Shutdown()
was called. I need to stop the bootstrapper code immediately following the call to shutdown.
Any ideas how to shutdown the application without creating the ResolutionFailedException
?
ResolutionFailedException exception details --> Resolution of the dependency failed, type = "SomeClass", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, SomeClass, is an interface and cannot be constructed. Are you missing a type mapping?
public class AgentBootstrapper : UnityBootstrapper
{
protected override void ConfigureContainer()
{
base.ConfigureContainer();
var eventRepository = new EventRepository();
Container.RegisterInstance(typeof(IEventRepository), eventRepository);
var dialog = new DialogService();
Container.RegisterInstance(typeof(IDialogService), dialog);
try
{
var someClass = new SomeClass();
Container.RegisterInstance(typeof(ISomeClass), SomeClass);
}
catch (ConfigurationErrorsException e)
{
dialog.ShowException(e.Message + " Application shutting down.");
**Application.Current.Shutdown();**
}
}
protected override System.Windows.DependencyObject CreateShell()
{
var main = new MainWindow
{
DataContext = new MainWindowViewModel(Container.Resolve<IEventRepository>(),
Container.Resolve<IDialogService>(),
Container.Resolve<ISomeClass>())
};
return main;
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)Shell;
Application.Current.MainWindow.Show();
}
}
Upvotes: 2
Views: 2740
Reputation: 521
This behaviour occurs, because you´re executing the OnStartup of your Application at this time. I suppose you do it like that:
protected override void OnStartup(StartupEventArgs e)
{
new AgentBootstrapper().Run();
}
The OnStartup has to be completed, before the App can shutdown, so the bootstrapper continues execution. You might throw another exception to get out of the Run():
... catch (ConfigurationErrorsException e)
{
dialog.ShowException(e.Message + " Application shutting down.");
throw new ApplicationException("shutdown");
}
And then catch it in the StartUp():
protected override void OnStartup(StartupEventArgs e)
{
try
{
new AgentBootstrapper().Run();
}
catch(ApplicationException)
{
this.Shutdown();
}
}
Upvotes: 2