CreationPolicy lifetime problem

Nov 5, 2009 at 3:08 PM

 

Hi,
Give the follow snippet of a test Part that I have created, along with the Main snippet below it, can anyone tell me why
my Parts are being processed in a "Sharable" manner as opposed to "NonShared". The whole things works except for the
CreationPolicy set on the Part.
Thanks, Mark
    [PartCreationPolicy(CreationPolicy.NonShared)]
    [Plugin("PluginA")]
    public class PluginA
        : IPlugin, IDisposable
    {
        private Message msg = new Message();
        private int count = 0;
        public PluginA()
        {
            Console.WriteLine("\tPlugin A created");
        }
        public Message Start(ref Message p_msg)
        {
            Console.WriteLine("Plugin A started..." + (++count));
            return this.msg;
        }
        public void Dispose() 
        {
            Console.WriteLine("Plugin A disposing");
        }
    }
        static void Main(string[] args)
        {
            DirectoryCatalog catalog = new DirectoryCatalog(Environment.CurrentDirectory, "*.dll");
            var container = new CompositionContainer(catalog);
            var pluginExports = container.GetExports<IPlugin, IPluginMetadata>();
            for (int i = 0; i < 3; i++)
            {
                Message request = new Message();
                IPlugin client = (from x in pluginExports
                                  where x.Metadata.Name == "PluginA"
                                  select x.Value).Single<IPlugin>();
                Message response = client.Start(ref request);
                client.Dispose();
            }
 
            System.Console.ReadLine();
        }

Hi,

 

Give the follow snippet of a test Part that I have created, along with the Main snippet below it, can anyone tell me why

my Parts are being processed in a "Sharable" manner as opposed to "NonShared". The whole things works except for the

CreationPolicy set on the Part.

 

Thanks, Mark

 

* the Part snippet...

    [PartCreationPolicy(CreationPolicy.NonShared)]

    [Plugin("PluginA")]

    public class PluginA

        : IPlugin, IDisposable

    {

        private Message msg = new Message();

        private int count = 0;

 

        public PluginA()

        {

            Console.WriteLine("\tPlugin A created");

        }

 

        public Message Start(ref Message p_msg)

        {

            Console.WriteLine("Plugin A started..." + (++count));

            return this.msg;

        }

 

        public void Dispose() 

        {

            Console.WriteLine("Plugin A disposing");

        }

    }

* the Main snippet...

        static void Main(string[] args)

        {

            DirectoryCatalog catalog = new DirectoryCatalog(Environment.CurrentDirectory, "*.dll");

            var container = new CompositionContainer(catalog);

            var pluginExports = container.GetExports<IPlugin, IPluginMetadata>();

 

            for (int i = 0; i < 3; i++)

            {

 

                Message request = new Message();

 

                IPlugin client = (from x in pluginExports

                                  where x.Metadata.Name == "PluginA"

                                  select x.Value).Single<IPlugin>();

 

                Message response = client.Start(ref request);

 

                client.Dispose();

            }

 

            System.Console.ReadLine();

        }

 

Developer
Nov 5, 2009 at 5:23 PM

When a part is marked as NonShared it means you will simply get a new instance everytime the container is queried. In your cause you only query the container once via container.GetExports and then try to reuse the same instance multiple times. You will need to query the container multiple times if you want to get multiple instances.

Nov 5, 2009 at 6:04 PM

Many thanks