Cannot access a disposed Object in Prism Mef

Topics: Prism v4 - WPF 4
Feb 4, 2014 at 1:42 PM

Please find the link below
and can any pls help me regarding this issue ?

Thanks and regards,
Feb 4, 2014 at 5:13 PM
Hi Sivalingam,

Based on the description in the thread you linked to, this problem only appeared once and you had not been able to reproduce it again. Is that correct?
Basically it seem that MEF encountered an error while trying to access its own CompositionContainer. I'm not aware of what can throw this kind of exception but in the mean time (until you are able to reproduce the error again) please check if there is any using block where you are using MEF's CompositionContainer or any part where you are invoking its Dispose method. MEF' CompositionContainer class implements IDisposable, so doing any of the above will dispose the container and possibly throw this exception.


Damian Cherubini
Feb 5, 2014 at 6:58 AM
Hi DCherubini,

I have used IDisposable in some of the parts of container in my application, but I dont know when the CompositionContainer gets disposed.

How can I prevent this Exception, can you pls help me ?
Feb 6, 2014 at 6:03 PM

I think the only way to efectively prevent this exception from apearing is to understand why the MEF's CompositionContainer is being disposed. Note that it should not matter if the parts you are exporting implement IDisposable or not as the problem is that the container itself is the one being disposed.
As far as I know, the container could be disposed in the following situations:
  • You are invoking the Dispose method of the CompositionContainer manually.
  • You are using the CompositionContainer as par of the scope of a using block, which disposes the container at the end.
  • The container could also be disposed if it's being garbage collected, but this would also mean that the container wouldn't be accesible at all.
Also, it would be helpful if you could provide more information about when this exception is appearing. Based on the error it seems the application failed when trying to create a region. Again, in your initial description you said that the error only appeared once. Have you been able to reproduce it again?


Damian Cherubini
Feb 7, 2014 at 1:12 PM
HI DCherubini,

Actually we are using the tab based application and passing the main container of the application to the tab viewmodel and set as parentcontainer of the tab viewmodel, so once the particular tab was closed, it is disposing the particular tab viewmodel container, since it is referring the same object of main container so it is also gets disposed and then when we try to load a view with content control in another tab. It is throwing the exception mentioned above and it reproducible all the times(10/10) when it is trying to access the regionmanager.regionname in the content control of the view.


In main container we are loading the common dlls of the application, whereas in the tabviewmodel we are loading the tab based dlls, so we are passing the main container and assign the same container as parent container to the tabviewmodel and creating a child contrainer from it using CreateChildContainer() in unity container, now we have changed to mef and in that CreateChildContainer() is not available, prob that may be the reason.

kindly suggest me the possible solution to handle this situation.

Feb 10, 2014 at 6:00 PM
Hi sivalingam,

Based on my understanding, you were working with Unity container without any issues, but now you are trying to migrate to MEF and the issue you described appears.
As you mentioned above, CreateChildContainer() method is not available on MEF Container. However, I am afraid I don't completely understand how are you creating the child container with MEF. It would be helpful if you could tell us how are you creating the container hierarchy. Or if you could provide a small sample that reproduces this issue, we could debug it and give you better support.

Nevertheless, I think the following sites may help you configure the nested containers properly:

I hope this helps,

Gabriel Ostrowsky
May 29, 2014 at 4:57 PM
      We have used MEF framework in our WPF application. Application is an MDI application built using tab controls. We have a parent container that hosts the base tab control and we have created child containers for each tab. We dispose child container of individual tab when deleting tabs. But, when we dispose child container that is generated at last, it is affecting all other child containers.  It throws the below exception when other child containers tries to export components. But, it is not throwing this exception when disposing containers that are generated before the last container.
Type : System.ComponentModel.Composition.CompositionException, System.ComponentModel.Composition, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) Cannot access a disposed object.
Object name: 'System.ComponentModel.Composition.Hosting.CompositionContainer'.

Resulting in: Activation error occured while trying to get instance of type DelayedRegionCreationBehavior, key ""

Resulting in: Set property 'Microsoft.Practices.Prism.Regions.RegionManager.RegionName' threw an exception.

Resulting in: An exception occurred while trying to create an instance of type 'VCA.Sparky.Modules.EMR.Views.ExamFindingsView'.

Resulting in: Cannot activate part 'VCA.Sparky.Modules.EMR.Views.ExamFindingsView'.
Element: VCA.Sparky.Modules.EMR.Views.ExamFindingsView --> VCA.Sparky.Modules.EMR.Views.ExamFindingsView --> AssemblyCatalog (Assembly="VCA.Sparky.Modules.EMR, Version=2.27.14034.13, Culture=neutral, PublicKeyToken=null")

Below is the code part we have in parent level bootstrapper:

public class Bootstrapper : MefBootstrapper
    private readonly EnterpriseLibraryLogger logger = new EnterpriseLibraryLogger();

    protected override void ConfigureAggregateCatalog()

        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Modules.Iconbar.IconbarModule).Assembly));


    protected ILoggerFacade LoggerFacade
        get { return logger; }

    ShellView view;
    protected override DependencyObject CreateShell()

        // Use the container to create an instance of the shell.
        return Container.GetExportedValue<ShellView>();


And, below is the code part we have in child level bootstrapper:

public class TabBootstrapper : MefBootstrapper
    public virtual CompositionContainer ParentContainer { get; set; }

    public int TabIndex { get; set; }

    protected override void ConfigureAggregateCatalog()
        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(TabBootstrapper).Assembly));
        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Modules.Appointment.AppointmentModule).Assembly));
        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Modules.Client.ClientModule).Assembly));
        Container = new CompositionContainer(AggregateCatalog);

    public TabViewModel ViewModel { get; set; }
    protected override DependencyObject CreateShell()

        return Container.GetExportedValue<TabContentView>();
   Any help/suggestion on handling this would be greatly appreciated.
Thanks in advance,
May 30, 2014 at 7:52 PM
Hi Kalai,
It will be helpful if you could provide us with a full repro sample of your solution, so that we can help you to identify the cause of this behavior you are mentioning.

You could upload your solution to OneDrive for example and post the link here.

Gabriel Ostrowsky