Search code examples
wpfprismunity-containerprism-7

Retrieve unity container type registration from configuration file in PRISM 7


The configuration section for the unity container:

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container>
  <register type="IProductInfo, Core.Interface" mapTo="Classes.ProductInfo, Core">
    <constructor>
      <param name="Name" value="Product Name" />
      <param name="Version" value="V1.2.65.30865" />
    </constructor>
  </register>
</container>

The types are registered in the App.xaml.cs file:

public partial class App : PrismApplication
{
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
      var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      UnityConfigurationSection section = (UnityConfigurationSection)config.GetSection("unity");
      section?.Configure(containerRegistry.GetContainer());
    }  
}

However, because of the container abstraction built in the PRISM 7 version, the container instance doesn't match with the required parameter in the Configure method. The following error is produced by the compiler:

Error CS1503 Argument 1: cannot convert from 'Unity.IUnityContainer' to 'Microsoft.Practices.Unity.IUnityContainer'

Question: How can I retrieve the correct container instance to pass as a parameter of the Configure method

P.s. There is already a similar question on the same issue on StackOverflow. However, the answer doesn't provide a solution to the instantiation problem described above.


Solution

  • You're using incompatible libraries. UnityConfigurationSection expects a Microsoft.Practices.Unity.IUnityContainer (from an old unity version, three-something, most likely), while GetContainer produces a Unity.IUnityContainer (from a rather recent unity).

    You should either upgrade your Unity.Configuration package or downgrade Prism...