Silverlight MEF composition

Aug 24, 2009 at 2:57 PM

NOTE: this is a MEF SL problem

I have an imprort that is not being fulfilled:

    <Import()> _
    Private wrapper As IHelloWorldWcfWrapper

The class in which this resides is exported

<Export()> _
Partial Public Class UserServices
I'll include the entire composition method below, but at the end of this method, I check that the value is in the container:
        Dim y = _container.GetExports(Of Acom.Common.IHelloWorldWcfWrapper)()
        Diagnostics.Debug.WriteLine(y.Count())
which returns 1. So the container appears to include the value, but its not found on the import. 
If it matters... there are a number of assemblies involved, all currently still in one xap. The interface is in one class library, the UserServices class in another. The implementation of IHelloWorld...that I'm trying to find is in the main SL assembly. 
I don't think its a stable composition problem beause there aren't further imports, and the class I'm trying to find does support the interface. Any other ideas?
   Private Function Compose() As Boolean
        Dim catalog = New AggregateCatalog()

        Dim initialCatalog = New PackageCatalog()
        catalog.Catalogs.Add(initialCatalog)

        initialCatalog.AddPackage(Package.Current)

        _container = New CompositionContainer(catalog)

        'Me.PackageCatalog = New PackageCatalog()
        'ac.Catalogs.Add(Me.PackageCatalog)

        Dim batch = New CompositionBatch()
        batch.AddPart(Me)
        ' // Add a wrapper for the Package Catalog into the container so it can be imported to load the albums
        'batch.AddPart(New PackageCatalogPart(pc))
        batch.AddExportedValue(Of CompositionContainer)(_container)

        Try
            _container.Compose(batch)
        Catch compositionException As CompositionException
            MessageBox.Show(compositionException.ToString())
            Return False
        End Try
        Dim x = _container.GetExports(Of Acom.AdministrationContracts.IUserServices)()
        Diagnostics.Debug.WriteLine(x.Count())
        Dim y = _container.GetExports(Of Acom.Common.IHelloWorldWcfWrapper)()
        Diagnostics.Debug.WriteLine(y.Count())
        Return True
    End Function
Aug 24, 2009 at 4:43 PM

Hi Kathleen

Private imports are not supported on Silverlight.

Aug 24, 2009 at 5:18 PM

Makes sense. And with stable composition that could buble up anywhere, although I don't think that is what's happening here.

Developer
Aug 24, 2009 at 9:54 PM

Glenn is correct that private imports are not supported on Silverlight. MEF can find non-public members via reflection but will not be able to set them becasue a security exception will be raised when you try to set a private member via reflection. Therefore stable composition will not help in this area, you will likely receive a CompositionException which will have a security error in it.

Also realize that the same is true for internal imports/exports, however in this case you can work around it by adding [InternalsVisibleTo("System.ComponentModel.Compsitoin")] to the assembly that has internal imports/exports.

We are working with the Silverlight team to see if there is anything we can do about this in the next version of Silverlight.