How to defer the InitializeShell() until Service is ready?

Topics: Prism v4 - WPF 4
May 19, 2011 at 9:53 AM

Hi guys,

 

I am converting the extisting WPF 4 project in our company to Prism and I have come across a big challenge.  I wonder if you may help me with that.

 

Within the App.xaml a Service is instantiated and initialized.  This takes up to 2 min.  At the same time a Splashscreen is shown on a different thread.

Once the Service is loaded an Event is raised that starts the Shell on a different thread.  

 

I need to create an instance of this Service as a singleton within IoC, so that whenever I need the service I would get the same instance.

Theoretically thats easy to so.  However I won't be able to do this in App.xaml, since I have no access to the Container yet. So I need to do this later within the Bootstrapper.

 But once the Bootstrapper is instantiated and running, it would execute the method InitializeShell(), which in turn starts off the Shell.

But the Shell needs the Service to be injected in its ViewModel and the Service is not even ready yet.  I need to make this method wait until the Service is successfully registered as a singleton within the ConfigureContainer() method, and only then once the Service's OnCompleted has been raised it should execute the InitializeShell() method.

How do I achieve this? :)

Many Thanks for your advice,

Houman

Developer
May 19, 2011 at 1:58 PM

Hi Houman,

The approach you're following of registering the service is the recommended possibility for having a service available in Prism, rather than creating it in the App.xaml.

As for deferring the initialization of the Shell, you could follow one of the following approaches:

  • You could override the Run method in your Bootstrapper to alter the application's initialization sequence so as to avoid calling the CreateShell and InitializeShell, and make your bootstrapper subscribe to the OnCompleted event in your service, so that when it's fully initialized, these methos are called.
  • You could also override the CreateShell and InitializeShell in your Bootstrapper, so that, instead of creating and showing the shell, they subscribe to your service's OnCompleted event to defer the aforementioned processes.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

May 20, 2011 at 4:18 PM

Hi Guido,

 

Many thanks for the tip.  I ended up abusing the ConfigureContainer() for my purpose and run a different method to run the Shell, whenever the Service is ready.  

I agree with you its better having the Service in Prism proper.  However due the nature of our application, where the service is not a real service but a Remoting object that the MainViewModel is subscribing to, I had no choice than doing it there for now without refactoring the application too much.

I am so happy that I suceeded introducing the Bootstrapper in our project.  We have so far used Prism as a MVVM framework, and the Bootstrapper with IoC would have been the next logical step.  That already would give us so much more power.  Hopefully in near future we start introducing the modules and make more use of Prism.  The learning curve is just a bit steep and it needs convincing. ;-)  

But we are getting there...

 

Thanks,

Houman