我有兩個模塊的Prism應用程序,其中一個(SecondModule)依賴於另一個(FirstModule)。我也使用log4net,我想爲每個模塊都有單獨的記錄器。因此我嘗試使用子容器。所述SecondModule使得這樣解決方案類型失敗的子容器,但在父容器中成功
public class Module : IModule
{
private readonly IUnityContainer container;
private readonly IRegionManager regionManager;
public Module(IUnityContainer container, IRegionManager regionManager)
{
this.regionManager = regionManager;
this.container = container.CreateChildContainer();
}
public void Initialize()
{
RegisterServices();
RegisterViews();
}
private void RegisterServices()
{
container.RegisterInstance(LogManager.GetLogger("PATSEARCH"));
//register all other services
container.RegisterType<IPatientSearchService, PatientSearchService>(new ContainerControlledLifetimeManager());
}
private void RegisterViews()
{
regionManager.RegisterViewWithRegion(RegionNames.MainMenu,() => container.Resolve<PatientSearch>());
//register other views
}
}
PatientSearch
是被配置爲自動線視圖模型作爲其數據源的用戶控制和這個視圖模型具有構造器參數IPatientSearchService
。現在的問題是,當棱鏡試圖視圖模型中解決IPatientSearchService
自動佈線失敗,出現異常
The current type, PatientSearchModule.Services.IPatientSearchService, is an interface and cannot be constructed. Are you missing a type mapping?
的事情是,如果我有this.container = container
內部模塊構造器一切正常更換this.container = container.CreateChildContainer();
。在這種情況下,子容器有什麼問題?
編輯:經過一番調查後我想我知道原因。問題是ViewModelLocator使用默認容器(在我的情況下是父容器)來解析視圖模型。所以新的問題是:我如何重新配置它以使用適當的容器?