MEF on Mono doesn't work properly?
Question
I've made a very simple MEF sample which runs on .NET, but doesn't work properly on Mono.
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;
namespace Vialis
{
class Program
{
[Import(typeof(ILedController))]
public List<ILedController> Controllers
{
get;
set;
}
static void Main(string[] args)
{
new Program();
}
public Program()
{
compose();
selectController();
Console.ReadLine();
}
private void compose()
{
var catalog = new DirectoryPartCatalog("controllers");
var container = new CompositionContainer(catalog);
container.AddPart(this);
container.Compose();
}
private void selectController()
{
Console.Clear();
Console.WriteLine("Please select the controller to use\n");
byte i = 0;
foreach (var controller in Controllers)
{
Console.WriteLine("\t{0}) {1}", i, controller.ToString());
i++;
}
Console.Write("\nYour selection: ");
var input = Convert.ToInt32(Console.ReadLine());
Controllers[input].DisplayText(10, 10, "Hello World");
}
}
}
This is the interface:
using System;
using System.Collections.Generic;
using System.Text;
namespace Vialis
{
public interface ILedController
{
void DisplayText(int x, int y, string text);
}
}
This is the first implementation:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;
namespace Vialis
{
[Export(typeof(ILedController))]
public class LyanController : ILedController
{
public void DisplayText(int x, int y, string text)
{
Console.SetCursorPosition(x, y);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(text);
}
}
}
The second implementation:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;
namespace Vialis
{
[Export(typeof(ILedController))]
public class VialisController : ILedController
{
public void DisplayText(int x, int y, string text)
{
Console.SetCursorPosition(x, y);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(text);
}
}
}
This is what happens on .NET (Windows):
Selecting the controllers:
But using Mono 2.2 the assemblies don't load:
Any suggestions ?
INFO: Google seems to be having some picasa web problems, that is why the images don't load.
The pictures show you that on Mac OS, no controllers are listed.
Solution
With the latest MEF release (Preview 4 - http://www.codeplex.com/MEF) it works just fine!
Since the bug is no longer relevant I voted to close this question.
OTHER TIPS
So I'm assuming that the exports are defined in external assemblies, because you use the DirectoryPartCatalog.. either there is an issue with the file path handling in the catalog or the problem is in the AttributedAssemblyPartCatalog / AttributedTypesPartCatalog
Internally the DirectoryPartCatalog wraps each discovered assembly into an AttributedAssemblyPartCatalog which in turn uses a AttributedTypesPartCatalog
Your best bet is to add the MEF project into your solution, instead of the dll, and set a break point in the greediest constructors of DirectoryPartCatalog & AttributedAssemblyPartCatalog and step until u find the problem
Unfortunatly I do not have a mono machine setup so I can't help more than that
Adding the interface and the two implementations to the application assembly works. So I'll have debug like you suggested, need to find a decent debugger for mono though.