Esempio n. 1
0
        public void DontExportICompositionServiceFromChildImportShouldShouldThrowCompositionException()
        {
            var rootCatalog  = new TypeCatalog(typeof(FromRoot));
            var childCatalog = new TypeCatalog(typeof(ClassRequiresICompositionService), typeof(ClassOptionallyImportsICompositionService));
            CompositionScopeDefinition scope = rootCatalog.AsScope(childCatalog.AsScope());
            var container = new CompositionContainer(scope);

            Assert.Throws <ImportCardinalityMismatchException>(() =>
            {
                FromRoot fromRoot = container.GetExportedValue <FromRoot>();
                Assert.Null(fromRoot);
            });
        }
Esempio n. 2
0
        public void ExportICompositionServiceFromChildImportShouldShouldSucceed()
        {
            var childCatalog = new TypeCatalog(typeof(ClassRequiresICompositionService), typeof(ClassOptionallyImportsICompositionService));
            var rootCatalog  = new TypeCatalog(typeof(FromRoot));
            CompositionScopeDefinition scope = rootCatalog.AsScope(childCatalog.AsScope());
            var container = new CompositionContainer(scope, CompositionOptions.ExportCompositionService);

            FromRoot fromRoot = container.GetExportedValue <FromRoot>();

            ExportLifetimeContext <ClassRequiresICompositionService> requiredService = fromRoot.Required.CreateExport();

            Assert.NotNull(requiredService.Value.CompositionService);

            ExportLifetimeContext <ClassOptionallyImportsICompositionService> optionalService = fromRoot.Optional.CreateExport();

            Assert.NotNull(optionalService.Value.CompositionService);
        }