Exceptions thrown by MEF throw ObjectDisposedException if the CompositionContainer has been disposed (as when the ```try``` is outside the ```using```. This is atypical: ```Exception```s aren't supposed to be ```IDisposable```.
Code:
```
using System;
using System.ComponentModel.Composition.Hosting;
namespace Segway.Service.Applications.MfgrMove.ListenerLogInterpreter
{
static class Program
{
#region Non-Public Class (Static) Constants and Variables
private static CompositionContainer _container;
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main ()
{
try
{
System.Windows.Forms.Application.EnableVisualStyles ();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault (false);
using (ApplicationCatalog catalog = new ApplicationCatalog ())
using (_container = new CompositionContainer (catalog))
{
try
{
using (IMainViewModel mainModel = GetInstance<IMainViewModel> ())
{
mainModel.StartModel ();
}
}
catch (Exception exc)
{
Console.WriteLine ("Inside the container and catalog");
DescribeException (exc);
throw;
}
}
}
catch (Exception exc)
{
Console.WriteLine ("Exception in Program:");
DescribeException (exc);
}
}
/// <summary>
/// Creates an instance of a type.
/// </summary>
/// <typeparam name="T">
/// The type (typically an interface) of the object to be created. By default, singletons
/// are created.
/// </typeparam>
/// <returns>
/// An instance of the type <typeparamref name="T"/>.
/// </returns>
public static T GetInstance<T> ()
{
return _container.GetExport<T> ().Value;
}
/// <summary>
/// Describes an exception and any inner exceptions to the Console.
/// </summary>
/// <param name="exc">
/// The exception to be described.
/// </param>
private static void DescribeException (Exception exc)
{
try
{
while (exc != null)
{
Console.WriteLine ("Exception: {0}", exc.GetType ().Name);
Console.WriteLine ("Message: {0}", exc.Message);
Console.WriteLine ("Stack Trace: {0}", exc.StackTrace);
exc = exc.InnerException;
if (exc != null)
{
Console.WriteLine ("Inner exception:");
}
}
}
catch (ObjectDisposedException e)
{
Console.WriteLine ("Accessing the exception raised an ObjectDisposedException.");
}
}
}
}
```
Results:
```
The thread 0x1b20 has exited with code 0 (0x0).
'Listener Log Interpreter.vshost.exe' (CLR v4.0.30319: Listener Log Interpreter.vshost.exe): Loaded 'C:\Users\brian.hetrick\Documents\Visual Studio 2015\Projects\Database Move\Listener Log Interpreter\bin\Debug\Listener Log Interpreter.exe'. Symbols loaded.
'Listener Log Interpreter.vshost.exe' (CLR v4.0.30319: Listener Log Interpreter.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Symbols loaded.
Exception thrown: 'System.ArgumentException' in Listener Log Interpreter.exe
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'System.ComponentModel.Composition.Primitives.ComposablePartException' in System.ComponentModel.Composition.dll
Exception thrown: 'System.ComponentModel.Composition.CompositionException' in System.ComponentModel.Composition.dll
Exception thrown: 'System.ComponentModel.Composition.CompositionException' in System.ComponentModel.Composition.dll
Exception thrown: 'System.ComponentModel.Composition.CompositionException' in mscorlib.dll
Inside the container and catalog
Exception: CompositionException
Message: The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) Value must be IMainView implementation
Parameter name: value
Resulting in: An exception occurred while trying to create an instance of type 'Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel'.
Resulting in: Cannot activate part 'Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel'.
Element: Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel --> Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel --> DirectoryCatalog (Path="C:\Users\brian.hetrick\Documents\Visual Studio 2015\Projects\Database Move\Listener Log Interpreter\bin\Debug\")
Resulting in: Cannot get export 'Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel (ContractName="Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.IMainViewModel")' from part 'Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel'.
Element: Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel (ContractName="Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.IMainViewModel") --> Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel --> DirectoryCatalog (Path="C:\Users\brian.hetrick\Documents\Visual Studio 2015\Projects\Database Move\Listener Log Interpreter\bin\Debug\")
Stack Trace: at System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition)
at System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportedValue(CatalogPart part, ExportDefinition export, Boolean isSharedPart)
at System.ComponentModel.Composition.Primitives.Export.get_Value()
at System.ComponentModel.Composition.ExportServices.Exception thrown: 'System.ComponentModel.Composition.CompositionException' in Listener Log Interpreter.exe
Exception thrown: 'System.ObjectDisposedException' in System.ComponentModel.Composition.dll
The thread 0x199c has exited with code 0 (0x0).
The thread 0x22f8 has exited with code 0 (0x0).
The program '[5880] Listener Log Interpreter.vshost.exe' has exited with code 0 (0x0).
GetCastedExportedValue[T](Export export)
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.Program.GetInstance[T]() in C:\Users\brian.hetrick\Documents\Visual Studio 2015\Projects\Database Move\Listener Log Interpreter\Program.cs:line 60
at Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.Program.Main() in C:\Users\brian.hetrick\Documents\Visual Studio 2015\Projects\Database Move\Listener Log Interpreter\Program.cs:line 28
Exception in Program:
Exception: CompositionException
Accessing the exception raised an ObjectDisposedException.
```
Code:
```
using System;
using System.ComponentModel.Composition.Hosting;
namespace Segway.Service.Applications.MfgrMove.ListenerLogInterpreter
{
static class Program
{
#region Non-Public Class (Static) Constants and Variables
private static CompositionContainer _container;
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main ()
{
try
{
System.Windows.Forms.Application.EnableVisualStyles ();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault (false);
using (ApplicationCatalog catalog = new ApplicationCatalog ())
using (_container = new CompositionContainer (catalog))
{
try
{
using (IMainViewModel mainModel = GetInstance<IMainViewModel> ())
{
mainModel.StartModel ();
}
}
catch (Exception exc)
{
Console.WriteLine ("Inside the container and catalog");
DescribeException (exc);
throw;
}
}
}
catch (Exception exc)
{
Console.WriteLine ("Exception in Program:");
DescribeException (exc);
}
}
/// <summary>
/// Creates an instance of a type.
/// </summary>
/// <typeparam name="T">
/// The type (typically an interface) of the object to be created. By default, singletons
/// are created.
/// </typeparam>
/// <returns>
/// An instance of the type <typeparamref name="T"/>.
/// </returns>
public static T GetInstance<T> ()
{
return _container.GetExport<T> ().Value;
}
/// <summary>
/// Describes an exception and any inner exceptions to the Console.
/// </summary>
/// <param name="exc">
/// The exception to be described.
/// </param>
private static void DescribeException (Exception exc)
{
try
{
while (exc != null)
{
Console.WriteLine ("Exception: {0}", exc.GetType ().Name);
Console.WriteLine ("Message: {0}", exc.Message);
Console.WriteLine ("Stack Trace: {0}", exc.StackTrace);
exc = exc.InnerException;
if (exc != null)
{
Console.WriteLine ("Inner exception:");
}
}
}
catch (ObjectDisposedException e)
{
Console.WriteLine ("Accessing the exception raised an ObjectDisposedException.");
}
}
}
}
```
Results:
```
The thread 0x1b20 has exited with code 0 (0x0).
'Listener Log Interpreter.vshost.exe' (CLR v4.0.30319: Listener Log Interpreter.vshost.exe): Loaded 'C:\Users\brian.hetrick\Documents\Visual Studio 2015\Projects\Database Move\Listener Log Interpreter\bin\Debug\Listener Log Interpreter.exe'. Symbols loaded.
'Listener Log Interpreter.vshost.exe' (CLR v4.0.30319: Listener Log Interpreter.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Symbols loaded.
Exception thrown: 'System.ArgumentException' in Listener Log Interpreter.exe
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'System.ComponentModel.Composition.Primitives.ComposablePartException' in System.ComponentModel.Composition.dll
Exception thrown: 'System.ComponentModel.Composition.CompositionException' in System.ComponentModel.Composition.dll
Exception thrown: 'System.ComponentModel.Composition.CompositionException' in System.ComponentModel.Composition.dll
Exception thrown: 'System.ComponentModel.Composition.CompositionException' in mscorlib.dll
Inside the container and catalog
Exception: CompositionException
Message: The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) Value must be IMainView implementation
Parameter name: value
Resulting in: An exception occurred while trying to create an instance of type 'Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel'.
Resulting in: Cannot activate part 'Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel'.
Element: Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel --> Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel --> DirectoryCatalog (Path="C:\Users\brian.hetrick\Documents\Visual Studio 2015\Projects\Database Move\Listener Log Interpreter\bin\Debug\")
Resulting in: Cannot get export 'Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel (ContractName="Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.IMainViewModel")' from part 'Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel'.
Element: Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel (ContractName="Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.IMainViewModel") --> Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.MainViewModel --> DirectoryCatalog (Path="C:\Users\brian.hetrick\Documents\Visual Studio 2015\Projects\Database Move\Listener Log Interpreter\bin\Debug\")
Stack Trace: at System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition)
at System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportedValue(CatalogPart part, ExportDefinition export, Boolean isSharedPart)
at System.ComponentModel.Composition.Primitives.Export.get_Value()
at System.ComponentModel.Composition.ExportServices.Exception thrown: 'System.ComponentModel.Composition.CompositionException' in Listener Log Interpreter.exe
Exception thrown: 'System.ObjectDisposedException' in System.ComponentModel.Composition.dll
The thread 0x199c has exited with code 0 (0x0).
The thread 0x22f8 has exited with code 0 (0x0).
The program '[5880] Listener Log Interpreter.vshost.exe' has exited with code 0 (0x0).
GetCastedExportedValue[T](Export export)
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.Program.GetInstance[T]() in C:\Users\brian.hetrick\Documents\Visual Studio 2015\Projects\Database Move\Listener Log Interpreter\Program.cs:line 60
at Segway.Service.Applications.MfgrMove.ListenerLogInterpreter.Program.Main() in C:\Users\brian.hetrick\Documents\Visual Studio 2015\Projects\Database Move\Listener Log Interpreter\Program.cs:line 28
Exception in Program:
Exception: CompositionException
Accessing the exception raised an ObjectDisposedException.
```