user1112560
user1112560

Reputation: 185

C# - MEF - Importing from a Portable Class Library

I can't seem to be able to load a class exported from a PCL DLL in a regular Windows DLL.

I am using the Nuget Package: http://www.nuget.org/packages/Microsoft.Composition/ (Microsoft Composition (MEF 2) 1.0.27)

Passing Code (inside a regular DLL):

using System.ComponentModel.Composition;

namespace Normal
{
    [Export]
    public class TestExport
    {
    }
}

Failing Code (inside a PCL DLL):

using System.Composition;

namespace PCL
{
    [Export]
    public class TestExport
    {
    }
}

Unit Test (Regular Unit Test Project):

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using PCL;

using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace UnitTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            AggregateCatalog catalog = new AggregateCatalog();

            catalog.Catalogs.Add(new AssemblyCatalog(typeof(PCL.TestExport).Assembly));
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(Normal.TestExport).Assembly));
            var container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);

            CompositionBatch batch = new CompositionBatch();
            batch.AddExportedValue(container);
            container.ComposeParts(batch);



            //Passes
            PCL.TestExport constructed = new PCL.TestExport();
            Normal.TestExport constructed2 = new Normal.TestExport();

            //Passes
            Normal.TestExport passes = container.GetExportedValue<Normal.TestExport>();


            //Fails
            PCL.TestExport e = container.GetExportedValue<PCL.TestExport>();
        }

    }

}

Upvotes: 2

Views: 792

Answers (1)

Daniel Plaisted
Daniel Plaisted

Reputation: 16744

Your regular DLL and unit test project are using System.ComponentModel.Composition, which is "MEF 1". MEF 1 doesn't know anything about the System.Composition attributes (MEF 2).

If you can use MEF 2 for all your projects, it should work.

Upvotes: 2

Related Questions