dedicated to write powerful yet elegant software by using minimum code and maximizing the benefits for all our clients.

Using MEF with Common Service Locator by navin 26/Oct/2010 02:52:00

"The Managed Extensibility Framework (MEF) simplifies the creation of extensible applications. MEF offers discovery and composition abilities that you can leverage to load application extensions." - (mef.codeplex.com)

"The common service locator library contains a shared interface for service location which application and framework developers can reference"  - (commonservicelocator.codeplex.com)

Wow!

How to get them both working together, is as below.

NOTE: I am assuming that the reader is aware of Dependency Injection pattern & the Common Service Locator.

The Steps:

  1. Download the CommonServiceLocator_MefAdapter.4.0.zip file & extract the MefServiceLocator class written by Glen Block
  2. Create a new ASP.Net MVC 2.0 Web Application Project
  3. Include the MefServiceLocator class into this project
  4. Open the Global.asax.cs file
  5. Make the following code change in the Application_Start event:
    protected void Application_Start(){
    RegisterDIContainer();
    AreaRegisteration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    }
  6. Write the RegisterDIContainer method as:
    protected void RegisterDIContainer(){
    var catalog = new AggregateCatalog(
    new AssemblyCatalog(Assembly.GetExecutingAssembly()),
    new DirectoryCatalog(Server.MapPath("~/Plugins"))
    );
    var composition = new CompositionContainer(catalog);
    composition.ComposeParts(this);
    var mef = new MefServiceLocator(composition);
    ServiceLocator.SetLocatorProvider(() => mef);
    }
  7. With this in place, you are now good to do:
    var controllers = ServiceLocator.Current.GetAllInstances<IController>();

Code on...

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Category: ASP.Net MVC | C# | Design Patterns | Web Development

Comments

24/Dec/2010 00:25:04

Incredibly enlightening many thanks, I presume your current subscribers may want further posts along these lines continue the excellent work.

Maxims

24/Dec/2010 23:31:57

Take care that you do not limit yourself. Lots of authors restrict themselves to what they believe they are able to do. Be sure you can gothat far as your mind will let you.

Ская

25/Dec/2010 01:05:08

Красивые проститутки москвы к вашим услугам.

Проститутки Москва

25/Dec/2010 02:31:42

Name of the young but very talented actress and dancer Sophia Arzhakovskoy in recent years has been on the pages of newspapers and TV screens.

Sofya Skya

31/Dec/2010 15:16:51

Благотворительная деательность Софьи Аржаковской.

Софья

02/Jan/2011 23:02:29

Супруга Сергея Веремеенко стала миссис мира 2006.

Веремеенко

03/Jan/2011 00:12:00

Софья Ская стала миссис мира 2006.

Ская

03/Jan/2011 16:40:23

Фотосессии Софья Ская - Аржаковской Веремеенко.

Фотосессии Ская

17/Jan/2011 19:21:05

Парное выступление Софьи Ская и Антона Варфоломеева.

Ская и Варфоломеев

17/Jan/2011 22:45:22

Лучшие индивидуaлки Киева и Украины для Вас.

Индивидуaлки Киев

22/Jan/2011 23:14:28

Лучшие проститутки Киева, проститутки Москвы, проститутки Питера и проститутки Росии с Украиной.

Проститутки

23/Jan/2011 00:00:19

Лучшие проститутки Питера и проститутки Росии ждут тебя.

Сексуальные проститутки

Comments are closed