Esempio n. 1
0
        public void SingleContainerPartReplacement()
        {
            var container = ContainerFactory.Create();
            var importPart = PartFactory.CreateImporter(true, "value1", "value2");

            CompositionBatch batch = new CompositionBatch();
            var export1Key = batch.AddExportedValue("value1", "Hello");
            batch.AddExportedValue("value2", "World");
            batch.AddPart(importPart);
            container.Compose(batch);

            Assert.AreEqual(2, importPart.ImportSatisfiedCount);
            Assert.AreEqual("Hello", importPart.GetImport("value1"));
            Assert.AreEqual("World", importPart.GetImport("value2"));

            importPart.ResetImportSatisfiedCount();

            batch = new CompositionBatch();
            batch.RemovePart(export1Key);
            batch.AddExportedValue("value1", "Goodbye");
            container.Compose(batch);

            Assert.AreEqual(1, importPart.ImportSatisfiedCount);
            Assert.AreEqual("Goodbye", importPart.GetImport("value1"));
            Assert.AreEqual("World", importPart.GetImport("value2"));
        }
Esempio n. 2
0
        private static void ComposeBatchTwo(CompositionContainer container)
        {
            var batch = new CompositionBatch();
            batch.RemovePart(a);
            c = batch.AddPart(new PluginC());
            batch.AddPart(logger);

            container.Compose(batch);
        }
Esempio n. 3
0
        public void BatchRemove_ShouldFireEvents()
        {
            var container = ContainerFactory.Create();
            var batch = new CompositionBatch();
            var exportPart = batch.AddExportedValue<object>("MyExport", new object());
            container.Compose(batch);

            var eventListener = new ExportProviderListener(container, container);

            batch = new CompositionBatch();
            batch.RemovePart(exportPart);
            eventListener.VerifyCompose(batch);
        }
Esempio n. 4
0
        public void AddRemovePart_NonSharedPart_ShouldCollectWholeObjectChain()
        {
            var container = GetContainer();

            var exportedValue = new NonSharedImporter();

            CompositionBatch batch = new CompositionBatch();
            var part = batch.AddPart(exportedValue);
            container.Compose(batch);
            batch = null;

            batch = new CompositionBatch();
            batch.RemovePart(part);
            container.Compose(batch);
            batch = null;

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesExpectedToBeCollected(
                exportedValue,
                exportedValue.AnyPartDisposable,
                exportedValue.AnyPartDisposableRecomposable,
                exportedValue.AnyPartRecomposable,
                exportedValue.AnyPartSimple);

            part = null;
            exportedValue = null;

            refTracker.CollectAndAssert();

            GC.KeepAlive(container);
        }
Esempio n. 5
0
        public void ImportIntoUntypedExportTest()
        {
            var container = ContainerFactory.Create();

            CompositionBatch batch = new CompositionBatch();
            batch.AddExportedValue("untyped", 42);

            var u = new UntypedExportImporter();
            var rb = AttributedModelServices.CreatePart(u);

            batch.AddPart(rb);
            container.Compose(batch);

            Assert.AreEqual(42, u.Export.Value);

            var us = new UntypedExportsImporter();
            batch = new CompositionBatch();
            batch.AddExportedValue("untyped", 19);
            batch.RemovePart(rb);
            batch.AddPart(us);
            container.Compose(batch);

            Assert.IsNotNull(us.Exports, "Should have an enumeration");
            Assert.AreEqual(2, us.Exports.Count(), "Should have 2 values");
        }
Esempio n. 6
0
        public void ExportsChanged_ExportRemoved_ShouldFireExportsChanged()
        {
            var container = CreateCompositionContainer();
            IEnumerable<string> changedNames = null;

            CompositionBatch batch = new CompositionBatch();
            var part = batch.AddExportedValue("MyExport", new object());
            container.Compose(batch);

            container.ExportsChanged += (sender, args) =>
            {
                Assert.AreSame(container, sender);
                Assert.IsNull(changedNames, "Ensure this event only fires once!");
                Assert.IsNotNull(args.AddedExports);
                Assert.IsNotNull(args.RemovedExports);
                Assert.IsNotNull(args.ChangedContractNames);
                changedNames = args.ChangedContractNames;
            };

            batch = new CompositionBatch();
            batch.RemovePart(part);
            container.Compose(batch);

            EnumerableAssert.AreEqual(changedNames, "MyExport");
        }
Esempio n. 7
0
        public void ComposeDisposableChildContainer()
        {
            var outerContainer = CreateCompositionContainer();
            Int32Importer outerImporter = new Int32Importer();

            CompositionBatch outerBatch = new CompositionBatch();
            var key = outerBatch.AddExportedValue("Value", 42);
            outerBatch.AddPart(outerImporter);
            outerContainer.Compose(outerBatch);
            Assert.AreEqual(42, outerImporter.Value, "Expected value imported from export");

            Int32Importer innerImporter = new Int32Importer();
            var innerContainer = new CompositionContainer(outerContainer);
            CompositionBatch innerBatch = new CompositionBatch();
            innerBatch.AddPart(innerImporter);

            innerContainer.Compose(innerBatch);
            Assert.AreEqual(42, innerImporter.Value, "Expected value imported from export");
            Assert.AreEqual(42, outerImporter.Value, "Expected value imported from export");

            outerBatch = new CompositionBatch();
            outerBatch.RemovePart(key);
            key = outerBatch.AddExportedValue("Value", -5);
            outerContainer.Compose(outerBatch);
            Assert.AreEqual(-5, innerImporter.Value, "Expected update value imported from export");
            Assert.AreEqual(-5, outerImporter.Value, "Expected updated value imported from export");

            innerContainer.Dispose();
            outerBatch = new CompositionBatch();
            outerBatch.RemovePart(key);
            key = outerBatch.AddExportedValue("Value", 500);
            outerContainer.Compose(outerBatch);
            Assert.AreEqual(500, outerImporter.Value, "Expected updated value imported from export");
            Assert.AreEqual(-5, innerImporter.Value, "Expected value not updated");
        }
Esempio n. 8
0
        public void RemovePart_PartInContainerQueueAsPartArgument_ShouldNotLeavePartInContainer()
        {
            const string contractName = "Contract";

            var exporter = PartFactory.CreateExporter(new MicroExport(contractName, 1));
            var importer = PartFactory.CreateImporter(contractName);
            var container = ContainerFactory.Create(exporter, importer);

            CompositionBatch batch = new CompositionBatch();
            batch.RemovePart(exporter);
            container.Compose(batch);

            Assert.IsNull(importer.Value);
            Assert.AreEqual(2, importer.ImportSatisfiedCount);
        }
Esempio n. 9
0
        public void SharedPart_DisposableRecomposabeImport_ShouldNotBeCollected()
        {
            var catalog = new TypeCatalog(typeof(SharedPartDisposableRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey = batch.AddExportedValue("Value", 21);
            container.Compose(batch);
            batch = null;

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesNotExpectedToBeCollected(
                container.GetExportedValue<SharedPartDisposableRecomposable>());

            refTracker.CollectAndAssert();

            // Lets make sure recomposition doesn't blow anything up here.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            container.Compose(batch);
            batch = null;

            var exportedValue = (SharedPartDisposableRecomposable)refTracker.ReferencesNotExpectedToBeCollected[0].Target;
            Assert.AreEqual(42, exportedValue.Value);

            container.Dispose();

            Assert.IsTrue(exportedValue.IsDisposed, "Any parts should be disposed with the container!");
        }
Esempio n. 10
0
        public void AnyPart_RecomposabeImport_ShouldNotBeCollected()
        {
            var catalog = new TypeCatalog(typeof(AnyPartRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey = batch.AddExportedValue("Value", 21);
            container.Compose(batch);
            batch = null;

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesNotExpectedToBeCollected(
                container.GetExportedValue<AnyPartRecomposable>());
            refTracker.CollectAndAssert();

            // Lets make sure recomposition doesn't blow anything up here.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            container.Compose(batch);
            batch = null;

            var exportedValue = (AnyPartRecomposable)refTracker.ReferencesNotExpectedToBeCollected[0].Target;
            Assert.AreEqual(42, exportedValue.Value);

            GC.KeepAlive(container);
        }
Esempio n. 11
0
        public void NonSharedPart_RecomposableImport_NoReference_ShouldBeCollected()
        {
            var catalog = new TypeCatalog(typeof(NonSharedPartRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey = batch.AddExportedValue("Value", 21);
            container.Compose(batch);
            batch = null;

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesExpectedToBeCollected(
                container.GetExportedValue<NonSharedPartRecomposable>());

            refTracker.CollectAndAssert();

            // Recompose just to ensure we don't blow up, even though we don't expect anything to happen.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            container.Compose(batch);
            batch = null;

            GC.KeepAlive(container);
        }
Esempio n. 12
0
 /// <summary>
 /// 卸载插件
 /// </summary>
 /// <param name="plugin"></param>
 /// <returns></returns>
 private bool UnLoadPlugin(IPlugin plugin)
 {
     bool isSuccess = false;
     try
     {
         var batch = new CompositionBatch();
         var part = AttributedModelServices.CreatePart(plugin);
         //var part = batch.AddExportedValue<IPlugin>(plugin);
         //var part2 = _container.GetExportedValues<IPlugin>().First();
         //Lazy<IPlugin> part3 = _container.GetExport<IPlugin>();
         //IPlugin tmp = part3.Value;
         batch.RemovePart(part);
         _container.Compose(batch);
         //_container.ReleaseExport(part3);
         isSuccess = true;
     }
     catch
     {
         isSuccess = false;
     }
     return isSuccess;
 }
        public void OptionalImportsOfExportReferenceTypesAreReboundToDefaultWhenExportIsRemoved()
        {
            var container = ContainerFactory.Create();
            var importer = new OptionalExport();

            CompositionBatch batch = new CompositionBatch();
            batch.AddPart(importer);
            var key = batch.AddExportedValue("ReferenceType", "Bar");

            container.Compose(batch);
            Assert.AreEqual(1, importer.ReferenceTypeSetCount);
            Assert.AreEqual("Bar", importer.ReferenceType.Value);

            batch = new CompositionBatch();
            batch.RemovePart(key);
            container.Compose(batch);

            Assert.AreEqual(2, importer.ReferenceTypeSetCount);
            Assert.IsNull(importer.ReferenceType);
        }
        public void OptionalImportsOfNullableValueTypesAreReboundToDefaultWhenExportIsRemoved()
        {
            var container = ContainerFactory.Create();
            var importer = new OptionalImport();

            CompositionBatch batch = new CompositionBatch();
            batch.AddPart(importer);
            var key = batch.AddExportedValue<int?>("NullableValueType", 10);

            container.Compose(batch);
            Assert.AreEqual(1, importer.NullableValueTypeSetCount);
            Assert.AreEqual(10, importer.NullableValueType);

            batch = new CompositionBatch();
            batch.RemovePart(key);
            container.Compose(batch);

            Assert.AreEqual(2, importer.NullableValueTypeSetCount);
            Assert.IsNull(importer.NullableValueType);
        }
Esempio n. 15
0
        public void Rejection_DefendPromisesOnceMade()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(Needy));

            var addBatch = new CompositionBatch();
            var removeBatch = new CompositionBatch();
            var addedPart = addBatch.AddPart(new NoImportPart());
            removeBatch.RemovePart(addedPart);

            // Add then remove should be fine as long as exports aren't used yet.
            container.Compose(addBatch);
            container.Compose(removeBatch);

            // Add the dependencies
            container.Compose(addBatch);

            // Retrieve needy which uses an export from addedPart
            var export = container.GetExportedValue<Needy>();

            // Should not be able to remove the addedPart because someone depends on it.
            ExceptionAssert.Throws<ChangeRejectedException>(() =>
                container.Compose(removeBatch));
        }
Esempio n. 16
0
        public void SelectiveRecompose()
        {
            var container = ContainerFactory.Create();
            var stableImporter = PartFactory.CreateImporter("stable");
            var dynamicImporter = PartFactory.CreateImporter("dynamic", true);
            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(stableImporter);
            batch.AddPart(dynamicImporter);
            var exportKey = batch.AddExportedValue("dynamic", 1);
            batch.AddExportedValue("stable", 42);
            container.Compose(batch);

            Assert.AreEqual(1, stableImporter.ImportSatisfiedCount);
            Assert.AreEqual(stableImporter.GetImport("stable"), 42);
            Assert.AreEqual(1, dynamicImporter.ImportSatisfiedCount);
            Assert.AreEqual(dynamicImporter.GetImport("dynamic"), 1);

            batch = new CompositionBatch();
            stableImporter.ResetImportSatisfiedCount();
            dynamicImporter.ResetImportSatisfiedCount();
            batch.RemovePart(exportKey);
            batch.AddExportedValue("dynamic", 2);
            container.Compose(batch);

            Assert.AreEqual(0, stableImporter.ImportSatisfiedCount, "Should not have imported the stable import part");
            Assert.AreEqual(stableImporter.GetImport("stable"), 42);
            Assert.AreEqual(1, dynamicImporter.ImportSatisfiedCount);
            Assert.AreEqual(dynamicImporter.GetImport("dynamic"), 2);
        }
Esempio n. 17
0
        public void NonSharedPart_RecomposableImport_WithReference_ShouldNotBeCollected()
        {
            var catalog = new TypeCatalog(typeof(NonSharedPartRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey = batch.AddExportedValue("Value", 21);
            container.Compose(batch);
            batch = null;

            var exportedValue = container.GetExportedValue<NonSharedPartRecomposable>();

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesNotExpectedToBeCollected(exportedValue);

            refTracker.CollectAndAssert();

            // Recompose should work because we are still holding a reference to the exported value.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            container.Compose(batch);
            batch = null;

            Assert.AreEqual(42, exportedValue.Value, "Value should have been recomposed");

            GC.KeepAlive(container);
        }
Esempio n. 18
0
        public void ParentedContainerPartReplacement()
        {
            var container = ContainerFactory.Create();
            CompositionBatch batch = new CompositionBatch();
            var importPart = PartFactory.CreateImporter(true, "value1", "value2");
            var exportKey = batch.AddExportedValue("value1", "Parent");

            var childContainer = new CompositionContainer(container);
            CompositionBatch childBatch = new CompositionBatch();
            childBatch.AddExportedValue("value2", "Child");
            childBatch.AddPart(importPart);

            Assert.AreEqual(0, importPart.ImportSatisfiedCount, "Should not import until outer scope is disposed");
            container.Compose(batch);
            childContainer.Compose(childBatch);

            Assert.AreEqual(2, importPart.ImportSatisfiedCount);
            Assert.AreEqual("Parent", importPart.GetImport("value1"));
            Assert.AreEqual("Child", importPart.GetImport("value2"));

            importPart.ResetImportSatisfiedCount();
            batch = new CompositionBatch();
            batch.RemovePart(exportKey);
            batch.AddExportedValue("value1", "New Parent");
            container.Compose(batch);

            Assert.AreEqual(1, importPart.ImportSatisfiedCount);
            Assert.AreEqual("New Parent", importPart.GetImport("value1"));
            Assert.AreEqual("Child", importPart.GetImport("value2"));
        }
Esempio n. 19
0
        public void NonSharedPart_DisposableRecomposabeImport_NoReference_ShouldNotBeCollected()
        {
            var catalog = new TypeCatalog(typeof(NonSharedPartDisposableRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey = batch.AddExportedValue("Value", 21);
            container.Compose(batch);
            batch = null;

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesNotExpectedToBeCollected(
                container.GetExportedValue<NonSharedPartDisposableRecomposable>());

            refTracker.CollectAndAssert();

            // Recompose just to ensure we don't blow up, even though we don't expect anything to happen.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            container.Compose(batch);
            batch = null;

            var exportedValue = (NonSharedPartDisposableRecomposable)refTracker.ReferencesNotExpectedToBeCollected[0].Target;
            Assert.AreEqual(42, exportedValue.Value, "Value shoudl ahve been recomposed.");
            
            GC.KeepAlive(container);
        }
Esempio n. 20
0
        public void RemovePart_PartNotInContainerAsPartArgument_ShouldNotCauseImportsToBeRebound()
        {
            const string contractName = "Contract";

            var exporter = PartFactory.CreateExporter(new MicroExport(contractName, 1));
            var importer = PartFactory.CreateImporter(contractName);
            var container = ContainerFactory.Create(exporter, importer);


            Assert.AreEqual(1, importer.Value);
            Assert.AreEqual(1, importer.ImportSatisfiedCount);

            var doesNotExistInContainer = PartFactory.CreateExporter(new MicroExport(contractName, 2));

            CompositionBatch batch = new CompositionBatch();
            batch.RemovePart(doesNotExistInContainer);
            container.Compose(batch);

            Assert.AreEqual(1, importer.ImportSatisfiedCount);
        }
Esempio n. 21
0
        public void AddRemovePart_SharedRoot_ShouldNotDisposeChain()
        {
            var container = GetContainer();

            var exportedValue = new SharedImporter();

            CompositionBatch batch = new CompositionBatch();
            var part = batch.AddPart(exportedValue);
            container.Compose(batch);

            batch = new CompositionBatch();
            batch.RemovePart(part);
            container.Compose(batch);

            Assert.IsFalse(exportedValue.AnyPartDisposable.IsDisposed);
            Assert.IsFalse(exportedValue.AnyPartDisposableRecomposable.IsDisposed);
        }
Esempio n. 22
0
        public void RemovePart_PartAlreadyRemovedAsPartArgument_ShouldNotThrow()
        {
            var exporter = PartFactory.CreateExporter(new MicroExport("Contract", 1));
            var container = ContainerFactory.Create(exporter);

            Assert.AreEqual(1, container.GetExportedValue<int>("Contract"));

            CompositionBatch batch = new CompositionBatch();
            batch.RemovePart(exporter);
            container.Compose(batch);

            Assert.IsFalse(container.IsPresent("Contract"));

            batch = new CompositionBatch();
            batch.RemovePart(exporter);
            container.Compose(batch);

            Assert.IsFalse(container.IsPresent("Contract"));
        }
Esempio n. 23
0
        public void GetReleaseExport_NonSharedPart_ShouldNotRecomposeAfterRelease()
        {
            var catalog = new TypeCatalog(typeof(NonSharedPartRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey = batch.AddExportedValue("Value", 21);
            container.Compose(batch);
            
            var export = container.GetExport<NonSharedPartRecomposable>();
            var exportedValue = export.Value;

            Assert.AreEqual(21, exportedValue.Value);

            container.ReleaseExport(export);

            // Recompose just to ensure we don't blow up, even though we don't expect anything to happen.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            container.Compose(batch);

            Assert.AreEqual(21, exportedValue.Value, "Value should not be recomposed after ReleaseExport is called on it.");
        }
Esempio n. 24
0
        public void RemoveValueTest()
        {
            var container = CreateCompositionContainer();
            CompositionBatch batch = new CompositionBatch();

            var key = batch.AddExportedValue("foo", "hello");
            container.Compose(batch);
            var result = container.GetExportedValue<string>("foo");
            Assert.AreEqual("hello", result, "Should get the correct value");

            batch = new CompositionBatch();
            batch.RemovePart(key);
            container.Compose(batch);

            Assert.IsFalse(container.IsPresent("foo"));

            batch = new CompositionBatch();
            batch.RemovePart(key);        // Remove should be idempotent
            container.Compose(batch);
        }
Esempio n. 25
0
        public void GetExportManualDisposeThenRecompose_NonSharedDisposableRecomposablePart_ShouldThrowComposition()
        {
            var catalog = new TypeCatalog(typeof(NonSharedPartDisposableRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey = batch.AddExportedValue("Value", 21);
            container.Compose(batch);
            
            var export = container.GetExport<NonSharedPartDisposableRecomposable>();
            var exportedValue = export.Value;

            Assert.AreEqual(21, exportedValue.Value);

            exportedValue.Dispose();

            // Recompose should cause a ObjectDisposedException.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);

            CompositionAssert.ThrowsError(
                              ErrorId.ImportEngine_PartCannotActivate,         // Cannot activate part because
                              ErrorId.ReflectionModel_ImportThrewException,         // Import threw an exception
                              RetryMode.DoNotRetry,
                              () => 
            {
                container.Compose(batch);
            });
        }
Esempio n. 26
0
        public void RemoveFromWrongContainerTest()
        {
            CompositionContainer d1 = CreateCompositionContainer();
            CompositionContainer d2 = CreateCompositionContainer();

            CompositionBatch batch1 = new CompositionBatch();
            var valueKey = batch1.AddExportedValue("a", 1);
            d1.Compose(batch1);

            CompositionBatch batch2 = new CompositionBatch();
            batch2.RemovePart(valueKey);
            // removing entry from wrong container, shoudl be a no-op
            d2.Compose(batch2);
        }
Esempio n. 27
0
        public void ChildContainerAddRemovePart_NonSharedRoot_ShouldDisposeChain()
        {
            var child = CreateParentChildContainerWithNonSharedImporter();

            var exportedValue = new NonSharedImporter();

            CompositionBatch batch = new CompositionBatch();
            var part = batch.AddPart(exportedValue);
            child.Compose(batch);

            batch = new CompositionBatch();
            batch.RemovePart(part);
            child.Compose(batch);

            Assert.IsTrue(exportedValue.AnyPartDisposable.IsDisposed);
            Assert.IsTrue(exportedValue.AnyPartDisposableRecomposable.IsDisposed);
        }