This project has moved and is read-only. For the latest updates, please go here.

Silverlight MEF composition

Aug 24, 2009 at 3: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)()
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()


        _container = New CompositionContainer(catalog)

        'Me.PackageCatalog = New PackageCatalog()

        Dim batch = New CompositionBatch()
        ' // 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)

        Catch compositionException As CompositionException
            Return False
        End Try
        Dim x = _container.GetExports(Of Acom.AdministrationContracts.IUserServices)()
        Dim y = _container.GetExports(Of Acom.Common.IHelloWorldWcfWrapper)()
        Return True
    End Function
Aug 24, 2009 at 5:43 PM

Hi Kathleen

Private imports are not supported on Silverlight.

Aug 24, 2009 at 6:18 PM

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

Aug 24, 2009 at 10: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.