C# 简单喷油器GetAllInstances引发Caliburn Micro异常

C# 简单喷油器GetAllInstances引发Caliburn Micro异常,c#,wpf,caliburn.micro,simple-injector,C#,Wpf,Caliburn.micro,Simple Injector,我曾经做过简单的注射器,但已经有将近两年的时间了。今天,当我尝试创建一个简单的WPF应用程序时。首先,我阅读了文档,因为这两个库都做了很多更改 我遇到了两个问题,比如“无法找到视图”,这些问题后来得到了解决,但现在我遇到了一个奇怪的问题。尝试启用记录器和所有功能,但不知道是Caliburn micro的问题还是简单的喷油器的问题 这是我的引导程序类: internal class AppBootstrapper : BootstrapperBase { public s

我曾经做过简单的注射器,但已经有将近两年的时间了。今天,当我尝试创建一个简单的WPF应用程序时。首先,我阅读了文档,因为这两个库都做了很多更改

我遇到了两个问题,比如“无法找到视图”,这些问题后来得到了解决,但现在我遇到了一个奇怪的问题。尝试启用记录器和所有功能,但不知道是Caliburn micro的问题还是简单的喷油器的问题

这是我的引导程序类:

 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);
        }
    }
内部类AppBootstrapper:BootstrapperBase
{
公共静态只读容器ContainerInstance=new Container();
公共AppBootstrapper()
{
LogManager.GetLog=type=>newdebuglogger(type);
初始化();
}
受保护的覆盖无效配置()
{
ContainerInstance.Register();
ContainerInstance.RegisterSingleton();
ContainerInstance.Register();
ContainerInstance.Verify();
}
启动时受保护的覆盖无效(对象发送方,System.Windows.StartupEventArgs e)
{
DisplayRootViewFor();
}
受保护的重写IEnumerable GetAllInstances(类型服务)
{
//运行应用程序时,此行抛出异常
//错误:
//-->SimpleInjector.dll中发生“SimpleInjector.ActivationException”类型的异常
//-->其他信息:找不到IEnumerable类型的注册。
//-->找不到IEnumerable类型的注册。
返回ContainerInstance.GetAllInstances(服务);
}
受保护的覆盖对象GetInstance(System.Type服务,字符串键)
{
返回ContainerInstance.GetInstance(服务);
}
受保护的重写IEnumerable SelectAssemblys()
{
返回新的[]{
Assembly.getExecutionGassembly()
};
}
受保护的覆盖空洞堆积(对象实例)
{
var registration=ContainerInstance.GetRegistration(instance.GetType(),true);
注册.注册.初始化instance(实例);
}
}

不确定我遗漏了什么?

Simple Injector v3包含几个突破性的更改。让你烦恼的是你的零钱。默认情况下,Simple Injector v3将不再将未注册的集合解析为空集合。正如您所注意到的,这会断开Caliburn的适配器

要解决此问题,您必须将
GetAllInstances
方法更改为以下内容:

protectedoverride IEnumerable GetAllInstances(类型服务)
{
IServiceProvider提供程序=容器状态;
Type collectionType=typeof(IEnumerable)。MakeGenericType(服务);
var services=(IEnumerable)provider.GetService(collectionType);
返回服务??可枚举的.Empty();
}

谢谢!工作得很好。甚至我也在考虑进行检查,但我认为我可能遗漏了一些为Caliburn设置容器的内容。我刚刚写了一篇关于此更新和修复的博客文章。感谢链接@vendettamit!!