I'm trying to figure out why one of my objects does not seem to be satisfying its Import. I think it may be the way I am using the container.ComposeParts() function but I have not been able to find much documentation with regards to it.
I have four projects
MEF.Service.Contracts
MEF.Service.Messaging (implements contracts)
MEF.Service.Core
MEF.Service.Console
MEF.Service.Contracts contains a simple interface called IMessageService with one function
public interface IMessageService
{
void SendMessage(string message);
}
MEF.Service.Messaging has one implementation of the above
[Export(typeof(IMessageService))]
public class ConsoleMessageService : IMessageService
{
#region IMessageService Members
public void SendMessage(string message)
{
Console.WriteLine(message);
}
#endregion
}
MEF.Service.Core has a class called ServiceManager that imports available services. In this example the IMessageService
public class ServiceManager
{
[Import]
public IMessageService MessageService { get; set; }
}
Finally, in the MEF.Service.Console application project I am creating the MEF container using a DirectoryCatalog. Then I create an instance of ServiceManager and call its MessageService property.
However at this point it fails with an object reference error.
Here is the code in the MEF.Service.Console project
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Run();
}
public void Run()
{
Compose();
ServiceManager manager = new ServiceManager();
manager.MessageService.SendMessage("Test Message");
Console.ReadLine();
}
private void Compose()
{
var catalog = new DirectoryCatalog(@".\");
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
MEF.Service.Console has references to the other three projects just to ensure the dlls are in the same folder during runtime.
I've examined the catalog and container after they are initialized and it does contain my ConsoleMessageService export as a part.
I'm trying to figure out why my [Import] MessageService in my ServiceManager is not getting satisfied.
Any help or pointers on composing parts and how Imports get satified would be appreciated.