big_tommy_7bb
big_tommy_7bb

Reputation: 1317

MEF Composition Errors: Only on some machines

I've a console application that uses MEF. It works fine locally and when deployed to a development machine. When deployed to the release machine it throws composition errors.

The development and release machine are both virtual sql server 2008 r2 machines, with the same spec and mostly the same software and components installed. There is a build and deployment process, but even copying the files from dev to release and running them results in the same error.

Are there any prerequisites I need for MEF that might be missing on the release machine, or permissions?

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 load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

Resulting in: An exception occurred while trying to create an instance of type 'XXX.XXX.Dispatch.EmailDispatcher'.

Resulting in: Cannot activate part 'XXX.XXX.Dispatch.EmailDispatcher'.
Element: Lifetime.CrmBroker.Dispatch.EmailDispatcher -->  XXX.XXX.Dispatch.EmailDispatcher -->  AssemblyCatalog (Assembly="XXX.XXX, Version=1.1.2264.871, Culture=neutral, PublicKeyToken=null")

Resulting in: Cannot get export 'XXX.XXX.Dispatch.EmailDispatcher (ContractName="XXX.XXX.Dispatch.IEntityTypeDispatcher")' from part 'XXX.XXX.Dispatch.EmailDispatcher'.
Element: XXXX.XXX.Dispatch.EmailDispatcher (ContractName="XXXX.XXX.Dispatch.IEntityTypeDispatcher") -->  XXX.XXX.Dispatch.EmailDispatcher -->  AssemblyCatalog (Assembly="XXX.XXX, Version=1.1.2264.871, Culture=neutral, PublicKeyToken=null")

Resulting in: Cannot set import 'XXX.XXXX.Dispatch.DispatcherRepository.Dispatchers (ContractName="XXX.XXX.Dispatch.IEntityTypeDispatcher")' on part 'XXX.XXX.Dispatch.DispatcherRepository'.
Element: XXX.XXX.Dispatch.DispatcherRepository.Dispatchers (ContractName="XXX.XXX.Dispatch.IEntityTypeDispatcher") -->  XXX.XXX.Dispatch.DispatcherRepository
 (System.ComponentModel.Composition.CompositionException)

Upvotes: 1

Views: 4384

Answers (3)

big_tommy_7bb
big_tommy_7bb

Reputation: 1317

Thanks for the replies guys, handy tips on debugging MEF.

It turns out that the problem was I didn't have Entity Framework 4.1 installed on the machine. MEF was swallowing up the exception that would have been thrown, I never found it, but I did a detailed comparison between the environments and just tried installing EF 4.1 to see if it worked.

Upvotes: 1

Wim Coenen
Wim Coenen

Reputation: 66723

Add some code to catch the CompositionException and get information about the root cause like this:

catch (CompositionException e)
{
    // "unable to load one or more of the requested types" hints at a
    // ReflectionTypeLoadException, so cast to that
    var loadException = (ReflectionTypeLoadException)e.Errors.First(); 

    // as the error said,
    // "Retrieve the LoaderExceptions property for more information"
    var cause = loadException.LoaderExceptions.First();

    // print, log or extract the information in some other way 
    Debug.Print(cause.Message);
}

It seems likely that it is just a missing dependency though.

Upvotes: 3

Audie
Audie

Reputation: 1470

Check your dynamically loaded type's constructor. MEF hides problems in your constructors behind MEF errors that don't help. Make sure you have error handling in the constructor, and then add some logging to catch the "real" exception.

Upvotes: 0

Related Questions