开发者

Microsoft Extensibility Framework - MEF CompositionContainer

开发者 https://www.devze.com 2023-02-04 01:27 出处:网络
I\'m in the process of building a Windows Service that implements MEF.The idea is that the serv开发者_JAVA技巧ice should manage sub-components and be pluggable, where any number of new components can

I'm in the process of building a Windows Service that implements MEF. The idea is that the serv开发者_JAVA技巧ice should manage sub-components and be pluggable, where any number of new components can be introduced at runtime without having to go through a full service recompile and deploy scenario. The components should be pluggable, meaning that as soon as I drop an output assembly into a designated folder on disc, the Windows Service should say "Hey, here's a new component to run..." and just do it's thing. That's the power of MEF. Very cool stuff so far.

I understand the concept of creating an AggregateCatalog and a CompositionContainer to import all available sub-components (assemblies). This is all good, and it works as expected when the Windows Service is intially started up. However, I'm eventually going to be introducing new components at runtime, so at some point I'm going to need the CompositionContainer to recognize any new output assemblies that are added.

My concern with the following is any implications on performance. Is it safe / efficient to put the CompositionContainer logic into a loop such as the following:

        [ImportMany(AllowRecomposition = true)]
        private IEnumerable<Lazy<IMySubComponentTask, IDictionary<string, object>>>                        
        MySubComponentTasks { get; set; }

        while (true)
        {
          //Create a general aggregate catalog
          var catalog = new AggregateCatalog();

          //Adds all the parts found in the same assembly as the Program class
          catalog.Catalogs.Add(new AssemblyCatalog(typeof(MySubComponentTaskFactory).Assembly));

          //Create a new directory catalog
          var directoryCatalog = new DirectoryCatalog(@".\Extensions");

          //Add the DLLs in the directory to the aggregate catalog
          catalog.Catalogs.Add(directoryCatalog);

          //Create the current composition container to create the parts
          var container = new CompositionContainer(catalog);

          //Fill the imports of this object
          try
          {
            container.ComposeParts(this);
          }
          catch (CompositionException compositionException)
          {
            Console.WriteLine(compositionException.ToString());
          }
       }

Any feedback or input is appreciated.

Thanks, Mike


No, I would not put the catalog instantiation into a loop. However, maybe a System.Timers.Timer or System.Threading.Timer to periodically check the plugin directory - whichever best suits your need.

Since your ImportMany allows recomposition you should be able to add new compositions at runtime:

Timer checkPlugin = new Timer();

//set CheckForPlugins as a timer callback or action
void CheckForPlugins()
{
    // add plugins catalog if found in directory
}

I found some additional information: Recomposition and constructors


Creating CompositionContainers should be cheap. Creating catalogs is not so cheap because the assemblies have to be inspected to find MEF parts. So you should create your catalog outside of the loop if possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号