title: How do I tell MEF to instantiate a single object for a given type per application?
body: <p>Lets say I have a class called FooController in which I have a property called Bar of type IBar (interface). I need to initialize Bar via MEF. However I need MEF to create only one instance of IBar type for the duration of the application (despite multiple calls to initialize it due to multiple requests) and make it available to all requests concurrently. Note that IBar implementations can be assumed thread safe.</p>
  
  <p>i.e.</p>
  
  <pre><code>public interface IBar
  {
      string Method();
  }
  
  [Export(typeof(IBar))]
  public class MyBar: IBar
  {
      public string dateTimeCreated;
  
      public MyBar()
      {
          System.Threading.Thread.Sleep(1000);
          dateTimeCreated = DateTime.Now.ToLongTimeString() + " ";
      }
  
      public string Method()
      {
          return dateTimeCreated;
      }
  }
  
  public class FooController : ApiController
  {
      [Import(typeof(IBar), RequiredCreationPolicy = CreationPolicy.Shared)]
      public IBar Bar { get; set; }
  
      public FooController()
      {
          //Assume CompositionContainer.ComposeParts call here
      }
  
      public string Get()
      {            
          return Bar.Method();
      }
  }
  </code></pre>
  
  <p>The problem is each time I call Get() on FooController, the returned time value changes. This means the MyBar object is being reinstantiated for each call. I basically need it to return the same value meaning I need to tell MEF to create only one instance of IBar in my application despite multiple requests.</p>
  
  <p>Thanks in advance.</p>