【问题标题】:Simple Injector GetAllInstances throwing exception with Caliburn Micro使用 Caliburn Micro 的简单注射器 GetAllInstances 引发异常
【发布时间】:2015-08-27 20:29:39
【问题描述】:

我曾在 Simple Injector 和 Caliburn micro 上工作过,但已经有将近 2 年的时间了。今天,当我尝试创建一个简单的 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);
        }
    }

不确定我在这里遗漏了什么?

【问题讨论】:

    标签: c# wpf caliburn.micro simple-injector


    【解决方案1】:

    Simple Injector v3 包含几个重大更改。困扰你的是issue #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>();
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-05
    • 2016-03-24
    • 2013-06-26
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多