How do I register types by convention with Unity with Prism?

I have a WPF application that uses packages Prism.Wpf

and Prism.Unity

NuGet (both 6.3.0

). I am currently registering types in the Unity container manually in the bootstrap class (see below) and everything works fine.

internal class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }

    protected override void InitializeShell()
    {
        Application.Current.MainWindow.Show();
    }

    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();

        // Register types
        Container.RegisterType<IDialogService, DialogService>(new ContainerControlledLifetimeManager());
    }
}

      

However, when I try to register types by convention, I get Microsoft.Practices.Unity.DuplicateTypeMappingException

when I register the types in the Unity container.

Register by conditional code:

protected override void ConfigureContainer()
{
    base.ConfigureContainer();

    // Register types by convention
    Container.RegisterTypes(
        AllClasses.FromLoadedAssemblies(),
        WithMappings.FromMatchingInterface,
        WithName.Default,
        WithLifetime.ContainerControlled);
}

      

Exception message:

An attempt to override an existing mapping was detected for type Prism.Regions.IRegionNavigationContentLoader with name "", currently mapped to type Prism.Unity.Regions.UnityRegionNavigationContentLoader, to type Prism.Regions.RegionNavigationContentLoader.

How do I register types by convention when using Prism and Unity?

+3


source to share


1 answer


Just swap Container.RegisterTypes(...);

andbase.ConfigureContainer();



UnityBootstrapper

will only register types that have not been previously registered, so you should be fine.

+4


source







All Articles