Пример #1
0
        /// <summary>
        /// MEF Bootstrap (MEF comes from System.ComponentModel.Composition in the GAC).
        /// <para>This will return a class containing all the application's aggregate roots.</para>
        /// </summary>
        public static ComposedDemoProgram Configure(params string[] pluginDirectories)
        {
            var catalogues = 
                pluginDirectories.Select<string, ComposablePartCatalog>(d=>new DirectoryCatalog(d)).
                Concat(new []{new AssemblyCatalog(Assembly.GetExecutingAssembly())}).ToList();

            var catalog = new AggregateCatalog(catalogues);
            try
            {
                var container = new CompositionContainer(catalog);

                var composedProgram = new ComposedDemoProgram();
                container.SatisfyImportsOnce(composedProgram);

                return composedProgram;
            }
            finally
            {
                catalog.Dispose();
                foreach (var cat in catalogues)
                {
                    cat.Dispose();
                }
            }
        }
Пример #2
0
 /// <summary>
 /// Releases the unmanaged resources used by the <see cref="DeploymentCatalog"/>
 /// and optionally releases the managed resources.
 /// </summary>
 /// <param name="disposing">
 /// true to release both managed and unmanaged resources; false to release only unmanaged
 /// resources.
 /// </param>
 protected override void Dispose(bool disposing)
 {
     try
     {
         if (disposing)
         {
             if (!this._isDisposed)
             {
                 AggregateCatalog catalog = null;
                 try
                 {
                     catalog          = this._catalog;
                     this._catalog    = null;
                     this._isDisposed = true;
                 }
                 finally
                 {
                     if (catalog != null)
                     {
                         catalog.Dispose();
                     }
                 }
             }
         }
     }
     finally
     {
         base.Dispose(disposing);
     }
 }
Пример #3
0
        public void AggregatingDisposedAndNotifications()
        {
            int changedCount = 0;
            int typesChanged = 0;

            EventHandler <ComposablePartCatalogChangeEventArgs> onChanged = delegate(object sender, ComposablePartCatalogChangeEventArgs e)
            {
                ++changedCount;
                typesChanged += e.AddedDefinitions.Concat(e.RemovedDefinitions).Count();
            };

            // Create our child catalogs
            AggregateCatalog[] mainChildren;
            AggregateCatalog[] otherChildren;
            TypeCatalog[]      componentCatalogs;
            CreateMainAndOtherChildren(out mainChildren, out otherChildren, out componentCatalogs);


            var parent = new AggregateCatalog(mainChildren);

            parent.Changed += onChanged;

            for (int i = 0; i < otherChildren.Length; i++)
            {
                parent.Catalogs.Add(otherChildren[i]);
            }

            Assert.AreEqual(otherChildren.Length, changedCount);
            Assert.AreEqual(otherChildren.Length * 3, typesChanged);

            changedCount = 0;
            typesChanged = 0;

            parent.Dispose();

            Assert.AreEqual(0, changedCount);
            Assert.AreEqual(0, typesChanged);

            //Ensure that the children are also disposed
            ExceptionAssert.ThrowsDisposed(otherChildren[0], () =>
            {
                otherChildren[0].Catalogs.Remove(componentCatalogs[0]);
            });

            //Ensure that the children are also disposed
            ExceptionAssert.ThrowsDisposed(otherChildren[4], () =>
            {
                otherChildren[4].Catalogs.Remove(componentCatalogs[0]);
            });

            Assert.AreEqual(0, changedCount);
            Assert.AreEqual(0, typesChanged);
        }
Пример #4
0
        /// <summary>Sets <see cref="T:System.ComponentModel.Composition.CompositionInitializer" /> to use a new container initialized with the specified catalogs.</summary>
        /// <returns>The new container.</returns>
        /// <param name="catalogs">The catalogs to load into the new container.</param>
        public static CompositionContainer Initialize(params ComposablePartCatalog[] catalogs)
        {
            AggregateCatalog catalog = new AggregateCatalog(catalogs);
            var container            = new CompositionContainer(catalog);

            try
            {
                Initialize(container);
            }
            catch
            {
                container.Dispose();
                catalog.Catalogs.Clear();
                catalog.Dispose();
                throw;
            }
            return(container);
        }
Пример #5
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    if (!this._isDisposed)
                    {
                        AggregateCatalog catalogs    = null;
                        bool             disposeLock = false;
                        try
                        {
                            using (new WriteLock(this._lock))
                            {
                                if (!this._isDisposed)
                                {
                                    disposeLock             = true;
                                    catalogs                = this._catalogCollection;
                                    this._catalogCollection = null;
                                    this._isDisposed        = true;
                                }
                            }
                        }
                        finally
                        {
                            if (catalogs != null)
                            {
                                catalogs.Dispose();
                            }

                            if (disposeLock)
                            {
                                this._lock.Dispose();
                            }
                        }
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Пример #6
0
        /// <summary>
        ///     This method can be used to initialize the global container used by <see cref="CompositionInitializer.SatisfyImports(object)"/>
        ///     in case where the default container doesn't provide enough flexibility. 
        ///     
        ///     If this method is needed it should be called exactly once and as early as possible in the application host. It will need
        ///     to be called before the first call to <see cref="CompositionInitializer.SatisfyImports(object)"/>
        /// </summary>
        /// <param name="catalogs">
        ///     An array of <see cref="ComposablePartCatalog"/> that should be used to initialize the <see cref="CompositionContainer"/> with.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="catalogs"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        ///     Either <see cref="Initialize(CompositionContainer)" /> or <see cref="Initialize(ComposablePartCatalog[])" />has already been called or someone has already made use of the global 
        ///     container via <see cref="CompositionInitializer.SatisfyImports(object)"/>. In either case you need to ensure that it 
        ///     is called only once and that it is called early in the application host startup code.
        /// </exception>
        public static CompositionContainer Initialize(params ComposablePartCatalog[] catalogs)
        {
            AggregateCatalog aggregateCatalog = new AggregateCatalog(catalogs);
            CompositionContainer container = new CompositionContainer(aggregateCatalog);
            try
            {
                CompositionHost.Initialize(container);
            }
            catch
            {
                container.Dispose();

                // NOTE : this is important, as this prevents the disposal of the catalogs passed as input arguments
                aggregateCatalog.Catalogs.Clear();
                aggregateCatalog.Dispose();

                throw;
            }

            return container;
        }
Пример #7
0
        /// <summary>
        ///     This method can be used to initialize the global container used by <see cref="CompositionInitializer.SatisfyImports(object)"/>
        ///     in case where the default container doesn't provide enough flexibility.
        ///
        ///     If this method is needed it should be called exactly once and as early as possible in the application host. It will need
        ///     to be called before the first call to <see cref="CompositionInitializer.SatisfyImports(object)"/>
        /// </summary>
        /// <param name="catalogs">
        ///     An array of <see cref="ComposablePartCatalog"/> that should be used to initialize the <see cref="CompositionContainer"/> with.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="catalogs"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        ///     Either <see cref="Initialize(CompositionContainer)" /> or <see cref="Initialize(ComposablePartCatalog[])" />has already been called or someone has already made use of the global
        ///     container via <see cref="CompositionInitializer.SatisfyImports(object)"/>. In either case you need to ensure that it
        ///     is called only once and that it is called early in the application host startup code.
        /// </exception>
        public static CompositionContainer Initialize(params ComposablePartCatalog[] catalogs)
        {
            AggregateCatalog     aggregateCatalog = new AggregateCatalog(catalogs);
            CompositionContainer container        = new CompositionContainer(aggregateCatalog);

            try
            {
                CompositionHost.Initialize(container);
            }
            catch
            {
                container.Dispose();

                // NOTE : this is important, as this prevents the disposal of the catalogs passed as input arguments
                aggregateCatalog.Catalogs.Clear();
                aggregateCatalog.Dispose();

                throw;
            }

            return(container);
        }
Пример #8
0
        public void AggregatingDisposedAndNotifications()
        {
            int changedCount = 0;
            int typesChanged = 0;

            EventHandler<ComposablePartCatalogChangeEventArgs> onChanged = delegate(object sender, ComposablePartCatalogChangeEventArgs e)
            {
                ++changedCount;
                typesChanged += e.AddedDefinitions.Concat(e.RemovedDefinitions).Count();
            };

            // Create our child catalogs
            AggregateCatalog[] mainChildren;
            AggregateCatalog[] otherChildren;
            TypeCatalog[] componentCatalogs;
            CreateMainAndOtherChildren(out mainChildren, out otherChildren, out componentCatalogs);


            var parent = new AggregateCatalog(mainChildren);
            parent.Changed += onChanged;

            for (int i = 0; i < otherChildren.Length; i++)
            {
                parent.Catalogs.Add(otherChildren[i]);
            }

            Assert.AreEqual(otherChildren.Length, changedCount);
            Assert.AreEqual(otherChildren.Length * 3, typesChanged);

            changedCount = 0;
            typesChanged = 0;

            parent.Dispose();

            Assert.AreEqual(0, changedCount);
            Assert.AreEqual(0, typesChanged);

            //Ensure that the children are also disposed
            ExceptionAssert.ThrowsDisposed(otherChildren[0], () =>
            {
                otherChildren[0].Catalogs.Remove(componentCatalogs[0]);
            });

            //Ensure that the children are also disposed
            ExceptionAssert.ThrowsDisposed(otherChildren[4], () =>
            {
                otherChildren[4].Catalogs.Remove(componentCatalogs[0]);
            });

            Assert.AreEqual(0, changedCount);
            Assert.AreEqual(0, typesChanged);
        }