2017-06-11 3 views
0

Ich versuche, ein LightInject Bootstrap-Programm für Prism zu erstellen, habe ich den Code des NinjectBootstrapper (source) kopiert und ersetzt Ninject des IKernel mit LightInject der IServiceContainer. Ich habe auch eine LightInjectServiceLocatorAdapter (Ninject version) erstellt und registriert.Erstellen eines LightInject Bootstrapper für Prism, fehlende Registrierung der Region Adapter

Der Registrierungs- und Service-Locator-Adapter funktioniert. Ich sehe, dass die Klasse RegionAdapterMappings erfolgreich aus dem Service-Locator abgerufen wird. Im Schritt ConfigureRegionAdapterMappings des Bootstrappers wird eine Instanz der Klasse SelectorRegionAdapter vom Service-Locator angefordert. Dies schlägt fehl. Die Klasse wurde nie registriert. Ich kann keinen Hinweis darauf in der NinjectBootstrapper finden. Wie wird diese Klasse vom Ninject Bootstrapper abgerufen?

Ich habe versucht, manuell die Klasse Registrierung (und ein paar weitere Region Adapter und die DelayedRegionCreationBehavior), aber dann nicht Prism mit einem UpdateRegionsException

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Prism.Regions.Behaviors.RegionCreationException: An exception occurred while creating a region with name 'Menu'. The exception was: System.Collections.Generic.KeyNotFoundException: The IRegionAdapter for the type System.Windows.Controls.ContentControl is not registered in the region adapter mappings. You can register an IRegionAdapter for this control by overriding the ConfigureRegionAdapterMappings method in the bootstrapper. 

ich Ihre eigenen jede Quelle des Umgang mit nicht finden kann, die Schaffung Hinzufügen DI-Container und ich habe keine Ahnung, welchen Schritt ich verpasse. Was mache ich falsch?

Ich konfiguriere meine ServiceContainer so (das Ninject entspricht der ConfigureKernel Methode).

protected virtual void ConfigureServiceContainer() 
{ 
    this.ServiceContainer.RegisterInstance<IServiceContainer>(this.ServiceContainer); 
    this.ServiceContainer.RegisterInstance(this.Logger); 
    this.ServiceContainer.RegisterInstance(this.ModuleCatalog); 

    if (!this.useDefaultConfiguration) 
     return; 

    this.ServiceContainer.Register<IServiceLocator, LightInjectServiceLocatorAdapter>(); 
    this.ServiceContainer.Register<IModuleInitializer, ModuleInitializer>(); 
    this.ServiceContainer.Register<IModuleManager, ModuleManager>(); 

    this.ServiceContainer.Register<RegionAdapterMappings, RegionAdapterMappings>(); 

    // The following 4 registrations are not in the NinjectBootstrapper 
    this.ServiceContainer.Register<SelectorRegionAdapter, SelectorRegionAdapter>(); 
    this.ServiceContainer.Register<ItemsControlRegionAdapter, ItemsControlRegionAdapter>(); 
    this.ServiceContainer.Register<ContentControlRegionAdapter, ContentControlRegionAdapter>(); 
    this.ServiceContainer.Register<DelayedRegionCreationBehavior, DelayedRegionCreationBehavior>(); 

    this.ServiceContainer.Register<IRegionManager, RegionManager>(); 
    this.ServiceContainer.Register<IEventAggregator, EventAggregator>(); 
    this.ServiceContainer.Register<IRegionViewRegistry, RegionViewRegistry>(); 
    this.ServiceContainer.Register<IRegionBehaviorFactory, RegionBehaviorFactory>(); 
    this.ServiceContainer.Register<IRegionNavigationJournalEntry, RegionNavigationJournalEntry>(); 
    this.ServiceContainer.Register<IRegionNavigationJournal, RegionNavigationJournal>(); 
    this.ServiceContainer.Register<IRegionNavigationService, RegionNavigationService>(); 
    this.ServiceContainer.Register<IRegionNavigationContentLoader, RegionNavigationContentLoader>(); 
} 
+0

Ich sehe zwei enge Stimmen, aber ich verstehe nicht den Grund warum? Irgendwelche Kommentare dazu? –

Antwort

0

Es stellte sich heraus, dass es zwei Dinge gab, die ich falsch gemacht habe.

Für die Registrierung in ConfigureServiceContainer verwendet der Ninject Bootstrapper eine Erweiterungsmethode public static void RegisterTypeIfMissing<A, B>(this IServiceContainer container, bool singletonScope), die einige Abhängigkeiten im Singleton-Bereich registriert.

Die andere Sache, die ich nicht verstanden habe, ist, dass Ninject glücklich jeden konkreten Typ auflösen wird, selbst wenn es nicht registriert ist. LightInject nicht. Ich löste schließlich das durch alle konkreten Klassen von Prism.WPF Registrierung, die in „Verhalten“ oder „Adapter“, um sich am Ende wie folgt:

// Register all concrete adapters and behaviors in the Prism.WPF assembly 
var wpfAssembly = typeof (SelectorRegionAdapter).Assembly; 
this.ServiceContainer.RegisterAssembly(wpfAssembly, (t0, t1) => t0.Name.EndsWith("Adapter") || t0.Name.EndsWith("Behavior")); 

Schließlich hatte ich alle Module Management von Prism zu Graben. LightInject erwartet, dass eine einzelne Klasse, die sich von ICompositionRoot erstreckt, in einer Assembly vorhanden ist und Typen von dort glücklich lädt. Das funktioniert gut für meinen Ansatz :).

Verwandte Themen