Visibility mismatch MEF / Unity

Jul 2, 2010 at 10:58 AM
Edited Jul 2, 2010 at 11:06 AM

Edit: It appears I posted too fast. Apparently, this is a MEF limitation in SL I didn't know about. Sorry 'bout that...

 

Hi there

I just wrote my first line of code using the Unity integration layer (which happened to be a unit test) and noticed that with Unity in play, resolving internal parts does no longer work. Btw: I recompiled the sources against SL4 and MEF RTM.

Resolving the Consumer class fails in the sample below, but works if I change the visibility of the importing property to public (stack trace below):

 

 

[Export]
public class Provider
{
}


public class Consumer
{
  [Import]
  internal Provider TheProvider { get; set; }
}

 

This might be a show stopper for existing code, as MEF nicely works with internal and private fields and properties. However, I have no idea whether this is something that cannot easily be solved in Unity...

Cheers,

Philipp

 

 

The exception:

Resolving Roche.LabUI.Services.Unity.Silverlight.Tests.Given_UnityContainer_When_Importing_Exports+Consumer,(none)
---> System.MethodAccessException: Fehler beim Versuch von Methode "Microsoft.Internal.ReflectionInvoke.SafeInvoke(System.Reflection.MethodInfo, System.Object, System.Object[])", auf Methode "Roche.LabUI.Services.Unity.Silverlight.Tests.Given_UnityContainer_When_Importing_Exports+Consumer.set_TheProvider(Provider)" zuzugreifen.
at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, RuntimeMethodHandleInternal method, RuntimeType parent, UInt32 invocationFlags)
at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, IRuntimeMethodInfo method, RuntimeType parent, UInt32 invocationFlags)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at Microsoft.Internal.ReflectionInvoke.SafeInvoke(MethodInfo method, Object instance, Object[] arguments)
at System.ComponentModel.Composition.ReflectionModel.ReflectionProperty.SetValue(Object instance, Object value)
at System.ComponentModel.Composition.ReflectionModel.ImportingMember.SetSingleMemberValue(Object instance, Object value)
at System.ComponentModel.Composition.ReflectionModel.ImportingMember.SetExportedValue(Object instance, Object value)
at System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePart.SetExportedValueForImport(ImportingItem import, ImportDefinition definition, Object value)
at System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePart.UseImportedValues[TImportDefinition](IEnumerable'1 definitions, Action'3 useImportValue, Boolean errorIfMissing)
at System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePart.SetNonPrerequisiteImports()
at System.ComponentModel.Composition.ReflectionModel.ReflectionComposablePart.Activate()
at System.ComponentModel.Composition.Hosting.ImportEngine.PartManager.TryOnComposed()
at System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImportsStateMachine(PartManager partManager, ComposablePart part)
at System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImports(PartManager partManager, ComposablePart part, Boolean shouldTrackImports)
at System.ComponentModel.Composition.Hosting.ImportEngine.SatisfyImports(ComposablePart part)
at System.ComponentModel.Composition.Hosting.ComposablePartExportProvider.<>c__DisplayClass2.<Compose>b__0()
at System.ComponentModel.Composition.Hosting.CompositionServices.TryInvoke(Action action)
at System.ComponentModel.Composition.Hosting.ComposablePartExportProvider.Compose(CompositionBatch batch)
at System.ComponentModel.Composition.Hosting.CompositionContainer.Compose(CompositionBatch batch)
at System.ComponentModel.Composition.AttributedModelServices.ComposeParts(CompositionContainer container, Object[] attributedParts)
at MefContrib.Integration.Unity.Strategies.ComposeStrategy.PostBuildUp(IBuilderContext context)
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable'1 resolverOverrides)
--- End of inner exception stack trace ---
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable'1 resolverOverrides)
at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)
at Roche.LabUI.Services.Unity.UnityServiceContainer.OnResolve(Type type, String name)
at Roche.LabUI.ServiceContainerBase.Resolve(Type type, String name)

Jul 2, 2010 at 11:07 AM
Edited Jul 2, 2010 at 11:07 AM

It appears I posted too fast. Apparently, this is a MEF limitation in SL I didn't know about.