roundcrisis
roundcrisis

Reputation: 17805

GetExportedValues<MyType> returns nothing, I can see the parts

I have a strange MEF problem, I tested this in a test project and it all seems to work pretty well but for some reason not working in the real project

This is the exporting code

    public void RegisterComponents()
    {

        _registrationBuilder = new RegistrationBuilder();
         _registrationBuilder
            .ForTypesDerivedFrom(typeof(MyType))
            .SetCreationPolicy(CreationPolicy.NonShared)
            .Export();

        var catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new AssemblyCatalog(typeof(MyType).Assembly, _registrationBuilder));


        var directoryCatalog = new DirectoryCatalog(PathToMyTypeDerived, _registrationBuilder);
        catalog.Catalogs.Add(directoryCatalog);

        _compositionContainer = new CompositionContainer(catalog);
        _compositionContainer.ComposeParts();

        var exports = _compositionContainer.GetExportedValues<MyType>();
        Console.WriteLine("{0} exports in AppDomain {1}", exports.Count(), AppDomain.CurrentDomain.FriendlyName);
    }

exports count is 0 :( Any ideas why?

IN the log file I have many of this

System.ComponentModel.Composition Information: 6 : The ComposablePartDefinition 'SomeOthertype' was ignored because it contains no exports.

Though I would think this is ok because I wasn' interested in exporting 'someOtherType'

UPDATE: I found this link but after debuging over it I am not wiser but maybe I m not following up properly.

Thanks for any pointers

Cheers

Upvotes: 3

Views: 1037

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174349

I just had the same problem and this article helped me a lot.

It describes different reasons why a resolve can fail. One of the more important ones is that the dependency of a dependency of the type you want to resolve is not registered.

What helped me a lot was the the trace output that gets written to the Output window when you debug your application. It describes exactly the reasons why a type couldn't be resolved.

Even with this output. you might need to dig a little bit, because I only got one level deep.

Example:

I wanted to resolve type A and I got a message like this:

System.ComponentModel.Composition Warning: 1 : The ComposablePartDefinition 'Namespace.A' has been rejected. The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced multiple composition errors, with 1 root causes. The root causes are provided below. Review the CompositionException.Errors property for more detailed information.

1) No exports were found that match the constraint: ContractName Namespace.IB RequiredTypeIdentity Namespace.IB

Resulting in: Cannot set import 'Namespace.A..ctor (Parameter="b", ContractName="namespace.IB")' on part 'Namespace A'. Element: Namespace.A..ctor (Parameter="b", ContractName="Namespace.IB") --> Namespace.A --> AssemblyCatalog (Assembly="assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=...")

But I clearly saw a part for Namespace.IB. So, in the debugger, I tried to resolve that one. And I got another trace output. This time it told me that my implementation of Namespace.IB couldn't be resolved because for one of its imports there was a missing export, so basically the same message as above, just with different types. And this time, I didn't find a part for that missing import. Now I knew, which type was the real problem and figure out, why no registration happened for it.

Upvotes: 3

Related Questions