title: MEF Composition error not propagating inner exception (except through message)
body: <p>I am working with MEF to load modules from different sources into my app. I have an example (code below) where I create a class that is composable which throws an exception in the constructor. That exception causes the composition to fail and thus throw an exception that says "why"... In the statement it says that the cause is an <code>InvalidCastException</code>... which is true.</p>
  
  <p>If I print the exception, I know why it fails, but how do I retrieve the ACTUAL original exception (<code>InvalidCastException</code>) thrown, in order for me to handle it properly -- rather than just a generic catch, which would miss other exceptions -- in the module that is asking for an instance of the said class? Querying the <code>CompositionException.Errors</code> doesn't give me the original exception either...</p>
  
  <h2><strong>CODE:</strong></h2>
  
  <pre><code>using System.ComponentModel.Composition;
  using Microsoft.Practices.ServiceLocation;
  
  [Export(typeof(A))]
  Class A
  {
      [ImportingConstructor]
      public A(ISomeinterface x, ISomeOtherInterface y)
      {
          //// Throw some exception (in my code it happens to be an InvalidCastException)
          throw new InvalidCastException ("Unable to cast object of type 'Sometype' to type 'Someothertype'.");  
      }
  }
  
  Class B
  {
      public B(IServiceLocator serviceLocator)
      {
          try
          {
              //// Here is where the exception would be thrown as an "ActivationException"
              A myAInstance = serviceLocator.GetInstance(A);
          }
          catch (ActivationException activationException)
          {
              //// Print original activation exception from trying to get the service
              System.Diagnostics.Debug.WriteLine(activationException);
  
              if (ex.InnerException is CompositionException)
              {
                  CompositionException compositionException = (CompositionException)activationException.InnerException;
  
                  //// *** Here is where I want to retrieve the InvalidCastException in order to handle it properly
                  //// *** Also, componentException.InnerException is "null" and so is the componentException.Errors[0].Exception.InnerException 
              }
              else
              {
                  throw;
              }
          }
      }
  }
  </code></pre>
  
  <h2><strong>OUTPUT:</strong></h2>
  
  <pre><code>Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type A, key "" ---&gt; System.ComponentModel.Composition.CompositionException: The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
  
  1) Unable to cast object of type 'Sometype' to type 'Someothertype'.
  
  Resulting in: An exception occurred while trying to create an instance of type 'A'.
  
  Resulting in: Cannot activate part 'A'.
  Element: A --&gt;  A --&gt;  DirectoryCatalog (Path="C:\path\to\code\")
  
  Resulting in: Cannot get export 'A (ContractName="A")' from part 'A'.
  Element: A (ContractName="A") --&gt;  A --&gt;  DirectoryCatalog (Path="C:\path\to\code\")
  
     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.ExportServices.GetCastedExportedValue[T](Export export)
     at System.ComponentModel.Composition.ExportServices.&lt;&gt;c__DisplayClass10`2.&lt;CreateSemiStronglyTypedLazy&gt;b__d()
     at System.Lazy`1.CreateValue()
     at System.Lazy`1.LazyInitValue()
     at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key) in c:\release\WorkingDir\PrismLibraryBuild\PrismLibrary\Desktop\Prism.MefExtensions\MefServiceLocatorAdapter.cs:line 73
     at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
     --- End of inner exception stack trace ---
     at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
     at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService]()
     at &lt;code&gt; line whatever...
  System.ComponentModel.Composition.CompositionException: The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
  </code></pre>