Why does Prism 4 register IRegionNavigationService as TransientLifetimeManager?

Ugh, leaky abstraction alert.

I was hoping to pop up some kind of exception message box whenever navigation failed. So, I retrieve the RegionNavigationService and register for the NavigationFailed event:

public void Initialization()
{
	var regionNavigationService = this.serviceLocator.GetInstance<IRegionNavigationService>();
	regionNavigationService.NavigationFailed += (sender, args) =>
	{
		var errorReportingService = this.serviceLocator.GetInstance<IErrorReportingService>();
		errorReportingService.ReportException(args.Error);
	}
}

Sadly, as soon as we fall out of scope of the Initialization method, regionNavigationService can be garbage collected.

The IRegionNavigationService is registered with the TransientLifetimeManager, meaning that each request for such an interface will merely instantiate a new object of the registered implementing type.

As a result, the event handler will never be fired because the regionNavigationService has been garbage collected.

Looking at the MSDN documentation for the IRegionNavigationService, NavigationFailed is just one of three events available on this interface, along with Navigated and Navigating.

These events can only be handled in close proximity to the original navigation request. You must retrieve the service from the container, register handlers for any events you are interested in and then make a navigation request.

What I was trying to do was supply a global navigation error handler, which will not work because the instance of the navigation service which I am listening to is different from the one which I am using to navigate from.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s