Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 0.88 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to initialize MEF ServiceLocator.Current?
  2. private void InitializeContainer()
  3.         {
  4.             var catalogs = new AggregateCatalog();
  5.  
  6.             var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
  7.             catalogs.Catalogs.Add(catalog);
  8.  
  9.             // Also adding Interactions project
  10.             catalog = new AssemblyCatalog(typeof(InteractionsService).Assembly);
  11.             catalogs.Catalogs.Add(catalog);
  12.  
  13.             // initialize the main application composition host (container)
  14.             CompositionHost.Initialize(catalogs);
  15.         }
  16.        
  17. this.InteractionsService = ServiceLocator.Current.GetInstance<IInteractionsService>();
  18.        
  19. private CompositionContainer _container
  20.  
  21. //your code
  22.  
  23. var batch = new CompositionBatch();
  24. batch.AddPart(this);
  25.  
  26. _container.Compose(batch);
  27.  
  28. //or more simply
  29. _container.ComposeParts(this) //if this method is supported