Quantcast
Channel: Managed Extensibility Framework
Viewing all articles
Browse latest Browse all 265

New Post: MEF instantiating event subscribers

$
0
0
Hello!
Thanks for your help! But I still have some issues . Have a look at the following code:-

Interface:
[InheritedExport]
    public interface IMessageConsumer
    {
        void Start();
    }
Subscriber:
[Export(typeof(IMessageConsumer))]
    public class MessageConsumer : IMessageConsumer
    {

        [Import]
        IEventAggregator eventAggregator { get; set; }

        public void Start()
        {
            eventAggregator.
                GetEvent<CompositePresentationEvent<SharedMessage>>().
                Subscribe(OnDataReceived, true);
            //keep waiting for message 
            Thread.Sleep(-1);
        }

        // should be public!
        public void OnDataReceived(SharedMessage txt)
        {
            MessageBox.Show(txt.Message);
        }
Publisher:
[Export(typeof(PublisherView))]
    public partial class PublisherView : UserControl
    {
        [Import]
        public IEventAggregator TheEventAggregator;

        public PublisherView()
        {
            InitializeComponent();
            button1.Click += new RoutedEventHandler(button1_Click);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // get a reference to the event from 
            // the event aggregator
            CompositePresentationEvent<SharedMessage> myMessageEvent =
                TheEventAggregator.GetEvent<CompositePresentationEvent<SharedMessage>>();

            //Set a message to be published
            SharedMessage message = new SharedMessage
            {
                Message = "Vivane"
            };

            //publish data via event aggregator
            myMessageEvent.Publish(message);
        }
    }
Helper class:
public class SharedMessage
    {
        public string Message { get; set; }
    }
Bootstrapper:
public void Start()
        {            
            //var consumers = this._CompositionContainer.GetExportedValues<IMessageConsumer>();
            var consumers = this.Container.GetExportedValues<IMessageConsumer>();

            // Start event subscribers who are now listening. These types are not injected so as to reduce coupling.
            foreach (var consumer in consumers)
            {
                var task = new Task(consumer.Start);
                task.Start();
            }
        }

protected override void ConfigureContainer()
        {            
            base.ConfigureContainer();
            //this._CompositionContainer = new CompositionContainer(this.AggregateCatalog);
            Start();
           
        }
Issues:
  1. The MessageConsumer shows received message two times. What is the main reason?
  2. In your example U called GetExportedValues method on static CompositionContainer class. I got this error:
    "An object reference is required for the non-static field, method, or property 'System.ComponentModel.Composition.Hosting.ExportProvider.GetExportedValues<T>"
I then decided to use this.Container.GetExportedValues<> as it has been shown above. Is it ok?

I'm so sorry for asking very basic questions. Im very new to Software dev and Prism|MEF as well.

Thanks.

user233223 wrote:
Hi,

Ensure your IStartable implementations are decorated with the appropriate export attribute, which in this case would be:
[Export(typeof(IStartable)]
public class Car : IStartable
{
    ...
}
This way the container will be able to import them (the IStartable's) when the container initializes.

You could also define an abstract Startable class that implements the IStartable and have all Startable (IStartable implementations) classes derive from that abstract base class which would allow your Startables to inherit the Start() method from the base class and not have to explicitly define that method for each implementation of IStartable.


It's me Tobias btw, lost my old password for that account.

Viewing all articles
Browse latest Browse all 265

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>