0
votes

I am using MEF to compose a number of components at run time but when I use a metadata attribute it double the number of parts in the collection.

When checking the container.ComposeParts I can see that the dll containing the 11 implementation of IStages is loaded once and only the 11 part required appear but when it resolves the Private Property Stages As IEnumerable(Of Lazy(Of IStages, IStagesMetadata)) the number of instances is doubled to 22. Looping through the collections I can see that they are duplicated by the ID in the metadata.

If I don't use the Metadataattribute on the export or import then I get the expected 11 parts.

Export Attribute

Imports System.ComponentModel.Composition

<MetadataAttribute(), AttributeUsage(AttributeTargets.Class, AllowMultiple:=False)>
Public Class StagesMetadataAttribute
    Inherits ExportAttribute

    Public Property StageID As Byte
    Public Property Version As String

    Public Sub New()
        MyBase.New(GetType(IStages))
    End Sub

End Class

Part Example

Imports System.ComponentModel.Composition
Imports VFRAME.QUALITY.GOODSIN.ESCALATE.INTERFACES

<Export(GetType(IStages))>
<StagesMetadata(StageID:=1, Version:="v1.0.0.0")>
Public Class Stage1
    Implements IStages

    Public Function ProcessEscalation(failure As InspectionFaultsModel) As InspectionFaultsModel Implements IStages.ProcessEscalation
        Return Nothing
    End Function

End Class

Import side constructor

Using catelog As New DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory)
    Using container As New CompositionContainer(catelog)
        Try
            container.ComposeParts(Me)
        Catch ex As CompositionException
            _compositionComplete = False
        End Try
    End Using
End Using

ImportMany

<ImportMany()>
Private Property Stages As IEnumerable(Of Lazy(Of IStages, IStagesMetadata))

Import metadate interface

Public Interface IStagesMetadata
    ReadOnly Property StageID As Byte
    ReadOnly Property Version As String
End Interface

Any idea what the problem is?

1

1 Answers

1
votes

Figured it out

If you decorate the part with the Export and StagesMetadata it creates 2 export definintions for the part as StagesMetadataAttribute inherits from ExportAttribute

<Export(GetType(IStages))>
<StagesMetadata(StageID:=1, Version:="v1.0.0.0")>
Public Class Stage1
    Implements IStages

    Public Function ProcessEscalation(failure As InspectionFaultsModel) As InspectionFaultsModel Implements IStages.ProcessEscalation
        Return Nothing
    End Function

End Class

Theres no accounting for stupidity.