Search code examples
c#mefmefedmvvm

Import allways returns null, but ImportConstructor works


I am trying to use MEF in my application, but I have problem with the Import.

    [Import (typeof(IUserServices ))]
    public IUserServices UserService { get; private set; }

This does not work and UserService is always null.

But using the ImportContstructor in the same class works perfectly:

    [ImportingConstructor ]
    public MainWindowVM(
        IUIVisualizerService uiVisualizer,
        IViewAwareStatus viewAwareStatus,
        IMessageBoxService messageBoxService, 
        IManager mwManager,
        TagItemModel tagModel,
        ILibraryModel  documentModel,
        ILibraryServices libraryServices,
        ILogServices logServices ,
        IUserServices userServices)

Can anybody help me in the issue. I already spend hours, but did not find any solution. Thanks!!!


Solution

  • I'm using ChinchV2 together with MefedMVVM to create the container. Here the code, which provides the export:

    [PartCreationPolicy(CreationPolicy.Shared)]
    [Export (typeof(IUserServices ))]
    public class TestUserServices:IUserServices 
    {
        public void GetSettings(Action<HubSettings, Exception> callback)
        {
            var dPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6);
            callback(new HubSettings {DataPath = dPath}, null);
        }
    }