Search code examples
c#eventsmef

Event wiring with mef


Can anyone help me with this problem? I'm working with the MEF framework, but I would like to communicate via events with "the module" to the plugin.

I've created a shared interface between those two parts this

public class BaseModule 
{          
    public event EventHandler<FeedBackArguments> SendFeedBack;
    public event EventHandler<ResultArguments> SendResult;


    public void InvokeFeedback(string message) {

        if (SendFeedBack != null)
            SendFeedBack(this, new FeedBackArguments{FeedbackString = message, FeedbackDate = DateTime. public void InvokeResult(bool passed, string resultMessage, string test) {

        if (SendResult != null)
            SendResult(this, new ResultArguments { Resultstring = resultMessage, Passed = passed, PassedTime = DateTime.Now, Teststring = test});

    }
}

this is my module:

[Export(typeof(IModule))]
    public class Tests : BaseModule, IModule
    {
     public void RememberDescription()
        {
            InvokeResult(true, "Please remember to upload a description","Rememberdescription");
        }

And I would like to import the module like this:

[Import(typeof(IModule))]

public IModule MEF;
  public void RunTests(string list)
    {

            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new DirectoryCatalog(folderlocator));

            try
            {
                CompositionContainer container = new CompositionContainer(catalog);
                container.ComposeParts(this);



                MEF.SendResult += MEF_SendResult;



            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

            }
        }

    }

    void MEF_SendFeedBack(object sender, FeedBackArguments e)
    {

    }

    void MEF_SendResult(object sender, ResultArguments e)
    {

    }

The event gets triggered in the module itself, but for some reason the event doesn't get fired or the listener doesn't work as it should. Help is very much appreciated.


Solution

  • The normal events won't work in MEF, I used the Event Aggregator instead which worked for me