Search code examples
asp.net-mvc-3mef

MEF not loading Imports


I have a main app that will have many plugins. Here is the PluginReader class in my main app:

Public Class PluginReader
 <ImportMany(GetType(IWebPlugin))>
    Private m_WebPlugins As IEnumerable(Of Lazy(Of IWebPlugin))

    Public Sub New()

    End Sub

    Public Property WebPlugins As IEnumerable(Of Lazy(Of IWebPlugin))
        Get
            Return m_WebPlugins
        End Get
        Set(value As IEnumerable(Of Lazy(Of IWebPlugin)))
            m_WebPlugins = value
        End Set
    End Property   
End Class

In my plugin I have this code:

<Export(GetType(IWebPlugin))>
<PartCreationPolicy(CreationPolicy.NonShared)>
Public Class PluginClass
    Implements IWebPlugin
    'Code to implement IWebPlugin here

My custom controller factor reads my plugin's exports correctly and I can see that the IWebPlugin is exported, but the ImportMany is not being populated.

If you need any clarification please let me know! Thanks!


Solution

  • I had to add a compose method to the PluginReader class.

     Private Sub Compose()
    
            Dim catalog As AggregateCatalog = New AggregateCatalog()
            catalog.Catalogs.Add(New DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory & "bin"))
            Dim container As New CompositionContainer(catalog)
            container.ComposeParts(Me)
    
        End Sub
    

    This was my constructor:

    Public Sub New()
            Compose()
        End Sub