How to handle interrupting Boostrapper

Jan 12, 2011 at 5:21 PM

In my Bootstrapper constructor, I am setting up a few things. Determining the environment (DEV,TEST,PROD), getting the connection string, finding the current user in the DB, etc.

I am checking for user access also. So if the user attempting to use the application does not exist in the database or does not have access, I want to pop up a message, and then exit the application.

So in my constructor, I am simply doing this for now:

if (User == null)
{
        System.Windows.MessageBox.Show("Sorry, you do not have access to this app.");
        this.Application.Shutdown();
        return;
}

I will not be using a MessageBox once I am finished, but this is just the prototype. The problem is, once it hits the MessageBox line, it immediately tries to run this from the CM Bootstrapper class:

protected virtual void OnStartup(object sender, StartupEventArgs e)
{
#if !WP7
    DisplayRootView();
#endif
}

Which results in this error, because it cannot create the Shell instance:

The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) Object reference not set to an instance of an object.
......
Resulting in: Cannot get export 'WindowsClients.MyApp.ViewModels.ShellViewModel 

Am I just going about this the wrong way for displaying a message to the user? My goal is to display a message to the user before it even tries to load the Shell view, and then exit the app. I am running into the same problem when there is any kind of error in the Bootstrapper, and I am trying to display another MessageBox to display the Error Message. Then the app needs to exit.

Coordinator
Jan 12, 2011 at 6:54 PM

You can probably accomplish what you want by overriding DisplayRootView and executing custom logic to check the user. If he is good to go, call the base implementation; if not show a message box, or whatever. If that doesn't work for you. You can write your own bootstrapper from scratch. The OOTB bootstrapper is just an easy way to get the framework set up. So, just look at its implementation and use that as a basis for your own.