Dynamic MEF isn’t thread safe by nature



Dynamic MEF isn’t thread safe by nature

this post focus a pitfall which you should be aware in order to avoid sporadic crashes.

In general most of MEF operation are operate once at the application initialization stage,

and it normally happens on a single thread.


this may not be true when you need to dynamically load MEF parts (using GetExportedValue or SatisfyImportsOnce).


unfortunately MEF underline is using non concurrent dictionary which is not thread safe for the above operation implementation.

therefore the following code will crash sporadically.

Code Snippet
  1. [Export] // exported type which will instantiate using MEF
  2. public class ExportedType {}
  4. public class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         var catalog = new AssemblyCatalog (typeof(Program).Assembly);
  9.         var container = new CompositionContainer(catalog);
  11.         for (int i = 0; i < 100; i++)
  12.         {
  13.             new Thread(() =>
  14.                 {   // get the exported part dynamically (instantiate by MEF)
  15.                     container.GetExportedValue<ExportedType>();
  16.                 }).Start();
  17.         }
  18.     }
  19. }

line 15: GetExportedValue call is a non protected critical section.



whenever using dynamic MEF techniques you should put a critical section synchronization over it.



Eran Stiller was the first one who’s point my intention to this issue.


kick it on DotNetKicks.com Shout it

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



  1. Jammer2016/03/18 ב 21:02

    Seems like the page where you link too for the code is no longer there …

  2. KatelynnK2016/12/30 ב 01:07

    Im thankful for the forum topic.Thanks Again. Will read on… http://e2te08uc.tumblr.com/ – Kitts

  3. Jennineciz2016/12/31 ב 02:10

    I am so grateful for your forum.Really looking forward to read more. Much obliged. http://4645ya4l.tumblr.com/ – Grandusky

  4. Jennineciz2016/12/31 ב 09:39

    Really Cool. http://4645ya4l.tumblr.com/ – Spell

  5. Jennineciz2016/12/31 ב 12:11

    I think this is a real great post. Fantastic. http://4645ya4l.tumblr.com/ – Kilburn

  6. Jesicahah2017/01/03 ב 16:04

    Really informative forum. http://n7sh3cas.tumblr.com/ – Earnheart

  7. Lonniefinny2017/01/06 ב 12:29

    Wow, great post.Really looking forward to read more. Want more. http://6926olpg.tumblr.com/ – Deitz

  8. Lonniefinny2017/01/06 ב 14:27

    Say, you got a nice forum post.Thanks Again. Keep writing. http://6926olpg.tumblr.com/ – Fingerhut

  9. eebest8 michael2018/01/09 ב 01:08

    “Great blog.Really thank you! Keep writing.”

  10. 12 inch Dob "Lif2018/01/11 ב 01:19

    “I appreciate you sharing this article post.Really thank you! Much obliged.”