我曾在Simple Injector和Caliburn micro工作过,但已经有近2年的时间了。今天当我尝试创建一个简单的WPF应用程序。首先,我最终阅读了文档,因为两个库都做了很多更改。
我遇到了几个问题,如"视图找不到",得到解决后,但现在我被一个奇怪的问题卡住了。尝试启用记录器和其他功能,但不知道是Caliburn micro的问题还是简单的注入器的问题。
这是我的bootstrapper类:
internal class AppBootstrapper : BootstrapperBase
{
public static readonly Container ContainerInstance = new Container();
public AppBootstrapper()
{
LogManager.GetLog = type => new DebugLogger(type);
Initialize();
}
protected override void Configure()
{
ContainerInstance.Register<IWindowManager, WindowManager>();
ContainerInstance.RegisterSingleton<IEventAggregator, EventAggregator>();
ContainerInstance.Register<MainWindowViewModel, MainWindowViewModel>();
ContainerInstance.Verify();
}
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
DisplayRootViewFor<MainWindowViewModel>();
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
// This line throwing is exception when running the application
// Error:
// ---> An exception of type 'SimpleInjector.ActivationException' occurred in SimpleInjector.dll
// ---> Additional information: No registration for type IEnumerable<MainWindowView> could be found.
// ---> No registration for type IEnumerable<MainWindowView> could be found.
return ContainerInstance.GetAllInstances(service);
}
protected override object GetInstance(System.Type service, string key)
{
return ContainerInstance.GetInstance(service);
}
protected override IEnumerable<Assembly> SelectAssemblies()
{
return new[] {
Assembly.GetExecutingAssembly()
};
}
protected override void BuildUp(object instance)
{
var registration = ContainerInstance.GetRegistration(instance.GetType(), true);
registration.Registration.InitializeInstance(instance);
}
}
不知道我在这里错过了什么?
Simple Injector v3包含了几个突破性的变化。你所困扰的是第98期的突破性变化。默认情况下,Simple Injector v3不再将未注册的集合解析为空集合。正如您所注意到的,这破坏了Caliburn的适配器。
要解决这个问题,您必须将GetAllInstances
方法更改为以下内容:
protected override IEnumerable<object> GetAllInstances(Type service)
{
IServiceProvider provider = ContainerInstance;
Type collectionType = typeof(IEnumerable<>).MakeGenericType(service);
var services = (IEnumerable<object>)provider.GetService(collectionType);
return services ?? Enumerable.Empty<object>();
}