开发者

Cannot Load implementations of interfaces from EXTERNAL DLL

This is my Compose part where I see the in dir Catalog THE RIGHT NUMBER that should be loaded , as well in asmCatalog.

public void Compose()
{
    var aggrCatalog = new AggregateCatalog();
    //A directory catalog
    var dirCatalog = new DirectoryCatalog(Path.GetDirectoryName(
        Assembly.GetExecutingAssembly().Location) + "\\Extensions", "*.dll");
    //An assembly catalog
    var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

    aggrCatalog.Catalogs.Add(dirCatalog);
    aggrCatalog.Catalogs.Add(asmCatalog);

    //Create a container THE CONTAINER DOES CONTAIN 5 PARTS AND
    // LOOK PERFECTLY OK. The 5 parts consist of 3 from this assembly 
    // and two from the external assembly
    var container = new CompositionContainer(aggrCatalog);

    // IT DOES NOT COMPOISE THE 2 PARTS THAT WERE LOADED FROM THE EXTERNAM ASSY
    container.ComposeParts(this);
}

I am using this interface and attributes:

namespace Q95Interface
{
    public interface IQ95_EventHandler
    {
        void HandleQ95Event();
        void HandleQ95Event(string s);
    }

    public interface IUIExtensionDetails
    {
        string Event_Group_Name { get; }
        string Event_Name { get; }
    }

    [MetadataAttribute]
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class Q95_EventHandlerAttribute : ExportAttribute
    {
        public Q95_EventHandlerAttribute() : base(typeof(IQ95_EventHandler)) { }

        public string Event_Group_Name { get; set; }
        public string Event_Name { get; set; }
    }
}

And the implementations that I want to import are ( the ones into the loc开发者_StackOverflow中文版al assembly look IDENTICAL and load and work perfectly )

namespace MefTest
{
    [Export(typeof(IQ95_EventHandler))]
    [ExportMetadata("EventType", "START_ORDER_EXTERNAL")]
    public class Q95_Event_Handler4 : IQ95_EventHandler
    {
        public void HandleQ95Event() 
        { 
            Console.WriteLine("HANDLE EVENT START_ORDER"); 
        }
        public void HandleQ95Event(string s) { Console.WriteLine(s); }
    }

    [Export(typeof(IQ95_EventHandler))]
    [ExportMetadata("EventType", "STOP_ORDER_EXTERNAL")]
    public class Q95_Event_Handler5 : IQ95_EventHandler
    {
        public void HandleQ95Event() 
        { 
            Console.WriteLine("\nHANDLE EVENT STOP_ORDER\n"); 
        }
        public void HandleQ95Event(string s) { Console.WriteLine(s); }
    }
}

I have separately compiled into a dll the interface and the attribute definitions


What is your [Import] defined as? I suspect it is likely because your exports don't export metadata. In fact, you've created a customised export attribute, and you're not using it.

Try changing your exports:

[Q95_EventHandler(Event_Group_Name = "Something", Event_Name = "Something Else")]
public class Q95_Event_Handler5 : IQ95_EventHandler
{

}

And ensure you import accordingly:

[ImportMany]
public IEnumerable<Lazy<IQ95_EventHandler, IUIExtensionDetails>> Handlers { get; set; }

It's worth looking at Daniel Plaisted's blog article entitled How to Debug and Diagnose MEF Failures to get an understanding of where you might be running into problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜