0

May I ask you to have a look at the code. I can not get any external methods. "operations" is null all the time "catalog" is Ok - I see that it sees all Dlls and methods in them. "AvailableNumberOfOperations" is always 0

public interface IOperation
{
    string Render(string parameters);
}
public class Extensions
{
    [ImportMany(typeof(IOperation))]
    private IEnumerable<Lazy<IOperation>> operations;

    private CompositionContainer _container;

    public int AvailableNumberOfOperations
    {
        get { return operations != null ? operations.Count() : 0; }
    }

    public string RenderModule(string moduleName, string args)
    {
        var result = "";
        if (AvailableNumberOfOperations != 0)
        {
            foreach (Lazy<IOperation> com in operations)
            {
                result = com.Value.Render(args);
            }
        }

        return result;
    }
    public void LoadModules()
    {
        // called first !!
        var catalog = new AggregateCatalog();

        string path = HttpContext.Current.Server.MapPath("~/App_Data/Extensions/");
        catalog.Catalogs.Add(new DirectoryCatalog(path));

        _container = new CompositionContainer(catalog);

        try
        {
            _container.ComposeParts();
        }
        catch (CompositionException compositionException)
        {
            Console.WriteLine(compositionException.ToString());
        }
    }

    [Export(typeof(OnePlace.IOperation))]
    [ExportMetadata("ModuleName", "HelloWorld")]
    public class HelloWorld : IOperation
    {
        public string Render(string parameters)
        {
            return "Hello World";
        }
    }

    [Export(typeof(OnePlace.IOperation))]
    [ExportMetadata("ModuleName", "SeeYouWorld")]
    public class SeeYouWorld : IOperation
    {
        public string Render(string parameters)
        {
            return "See You World";
        }
    }
}

And the external DLL is like this one. I have two method in main add and one is in external dll I have to write all these comments to be able to insert all the code. Sorry. It will be really helpful if you could explain me where is my mistake - Thank you ! I've never use MEF before.

namespace SimpleMenu
{
  [Export(typeof(OnePlace.IOperation))]
  [ExportMetadata("ModuleName", "Menu")]
  public class Menu : OnePlace.IOperation
  {
    public string Render(string parameters)
    {
        return "menu 13";
    }
  }
}

enter image description here

Serge
  • 62
  • 8

1 Answers1

0

Your instance of Extensions seems to be created outside MEF, so the CompositionContainer has no idea that there are any imports to satisfy.

You can workaround it by calling: _container.SatisfyImportsOnce(this):

    public void LoadModules()
    {
        // called first !!
        var catalog = new AggregateCatalog();

        string path = HttpContext.Current.Server.MapPath("~/App_Data/Extensions/");
        catalog.Catalogs.Add(new DirectoryCatalog(path));

        _container = new CompositionContainer(catalog);

        try
        {
            _container.ComposeParts();
            _container.SatisfyImportsOnce(this);
        }
        catch (CompositionException compositionException)
        {
            Console.WriteLine(compositionException.ToString());
        }
    }

See similar answer: SatisfyImportsOnce vs ComposeParts