I am writing a WPF application using PRISM 4.1 & Unity. The application will have every UI component configurable, including the Shell itself!
E.g. I have an interface called IShell
. Consumers of the app can have their own implementation of IShell
if they are not satisfied with my default implementation which has Regions and Views defined in some fixed way.
Now from my Bootstrapper class (which inherits UnityBootstrapper
), I want to know the type registered for IShell
with the Unity container. The overriden CreateShell
will return configured type of the IShell
.
My App.config
looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity>
<container>
<register type="Interfaces.IShell, Interfaces" mapTo="PrismApp.Shell, PrismApp"/>
</container>
</unity>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
In Bootstrapper class, I have following code:
public class PrismAppBootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
var obj = ServiceLocator.Current.GetInstance<IShell>() as DependencyObject;
return obj;
}
}
In the WPF application's App.xaml.cs, I am instantiating PrismAppBootstrapper
:
PrismAppBootstrapper prismAppBootstrapper = new PrismAppBootstrapper();
prismAppBootstrapper.Run();
However, when I run this code, I get the Exception : "InvalidOperationException - The current type, Interfaces.IShell, is an interface and cannot be constructed. Are you missing a type mapping?"
How to solve this problem? Why does application fail to know the registered type of IShell which is there in app.config file?
Keeping the Shell configurable is possible with the Unity container.
The problem was that my Unity Container was not properly configured to read mapping/registrations from the app.config file. So it failed to know the mapping of
IShell
at run time.I had to override other methods along with
CreateShell
in myPrismAppBootstrapper
class.E.g.