Example #1
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.");
        }
Example #2
0
    private void Run()
    {
        var aggregateCatalog = new AggregateCatalog();

        using (var container = new CompositionContainer(aggregateCatalog))
        {
            container.ComposeParts(this);
            //Check if the field is injected. It shouldn't be since the
            //NonShared type is not known to the container yet..
            Console.WriteLine("NonShared field {0}", this._nonShared != null ? "exists" : "does not exist");
            //Add the NonShared type to a type catalog.
            var typeCatalog = new TypeCatalog(typeof(NonShared));
            //Add the TypeCatalog to the AggregateCatalog.
            aggregateCatalog.Catalogs.Add(typeCatalog);
            //Check if the field is injected. This time it should be.
            Console.WriteLine("NonShared field {0}", this._nonShared != null ? "exists" : "does not exist");

            if (this._nonShared != null)
            {
                //Access the lazy object so it gets a value.
                this._nonShared.Value.ToString();
                //Release the part. The Dispose method should be called.
                container.ReleaseExport <NonShared>(this._nonShared);
            }
        }
    }
Example #3
0
        private DoStepsHostView GetDoStepsView(ContentURI uri,
                                               string extensionTypeName)
        {
            DoStepsHostView hostview = null;

            try
            {
                //directory storing extensions (webproject apppath (c:\Devtreks)
                //extension projects use the project's post build event to copy their dlls there
                string sExtensionRoot = DataHelpers.AppSettings.GetExtensionsRelPath(uri);
                CompositionContainer container
                    = GetCompositionContainerFromDirectory(sExtensionRoot);
                container.ComposeParts(this);
                foreach (var calculatorView in calculatorViews)
                {
                    //the export attributes will change in future upgrades
                    //extensiontypename (a parameter stored in db) determines extension to run
                    if (calculatorView.Metadata.CONTRACT_TYPE
                        == CALULATOR_CONTRACT_TYPES.defaultcalculatormanager &&
                        calculatorView.Metadata.CalculatorsExtensionName.ToLower()
                        == extensionTypeName.ToLower())
                    {
                        hostview = calculatorView.Value;
                        container.ReleaseExport(calculatorView);
                        break;
                    }
                }
            }
            catch (CompositionException cex)
            {
                uri.ErrorMessage = cex.ToString();
            }
            return(hostview);
        }
Example #4
0
 /// <summary>
 /// <see cref="M:System.IDisposable.Dispose"/>
 /// </summary>
 /// <param name="disposing">False if unmanaged resources must be disposed, false otherwise.</param>
 protected virtual void Dispose(bool disposing)
 {
     if (!disposed)
     {
         compositionContainer.ReleaseExport(application);
         application = null;
     }
     disposed = true;
 }
Example #5
0
        public override void ReleaseController(IController controller)
        {
            lock (_syncRoot)
            {
                var export = _exports[controller];
                _exports.Remove(controller);

                _container.ReleaseExport(export);
            }
            base.ReleaseController(controller);
        }
Example #6
0
        public void ReleaseParsleyAsTransientDisposesParsley()
        {
            var catalog   = new TypeCatalog(typeof(Ploeh.Samples.Menu.Mef.Attributed.Lifetime.NonShared.Parsley));
            var container = new CompositionContainer(catalog);

            var x          = container.GetExport <IIngredient>();
            var ingredient = x.Value;

            container.ReleaseExport(x);

            var parsley = Assert.IsAssignableFrom <Parsley>(ingredient);

            Assert.True(parsley.IsDisposed);
        }
Example #7
0
        static void Main(string[] args)
        {
            AssemblyCatalog      catalog   = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            CompositionContainer container = new CompositionContainer(catalog);

            var classA   = container.GetExport <ClassA>();
            var instance = classA.Value;

            //then we will use the generic ReleaseExport method and gie it the lazy instance we got from the container.
            container.ReleaseExport <ClassA>(classA);
            //Finally we will dispose the container.
            container.Dispose();

            Console.ReadKey();
        }
Example #8
0
 public void Dispose()
 {
     if (!_stopped)
     {
         _stopped = true;
         _service?.RemoveListener(this);
         _tcpListener?.Stop();
         Authenticator = null;
         if (null != _mefContainer)
         {
             if (null != _export)
             {
                 _mefContainer.ReleaseExport(_export);
             }
             _mefContainer.Dispose();
         }
     }
 }
Example #9
0
 public static T GetExportedInstance <T>(this CompositionContainer container)
 {
     try
     {
         var export = container.GetExport <T>();
         if (export != null)
         {
             T instance = export.Value;
             container.ReleaseExport(export);
             return(instance);
         }
         return(default(T));
     }
     catch (Exception x)
     {
         Console.WriteLine(x.ToString());
         throw;
     }
 }
Example #10
0
 /// <summary>
 /// 從容器中釋放Export對象
 /// </summary>
 /// <param name="export">要釋放的Export對象</param>
 public void ReleaseExport(Export export)
 {
     _container.ReleaseExport(export);
 }
Example #11
0
 /// <summary>
 /// Releases an export
 /// </summary>
 /// <typeparam name="T">Type to resolve</typeparam>
 /// <param name="export">Object instance to release</param>
 public static void Release <T>(Lazy <T> export)
 {
     Container.ReleaseExport <T>(export);
 }