Importing MEF-Plugins into MVC Controllers

Posted by Marks on Stack Overflow See other posts from Stack Overflow or by Marks
Published on 2010-05-17T13:40:21Z Indexed on 2010/05/17 14:20 UTC
Read the original article Hit count: 456

Filed under:
|
|
|
|

Hello. There are several examples of using MEF to plugin whole controller/view packages into a MVC application, but i didn't found one, using MEF to plugin funcional parts, used by other controllers.

For example, think of a NewsService with a simple interface like

interface INewsService
{
    List<NewsItem> GetAllNews();
}

That gets news wherever he wants, and returns them in a List of NewsItems. My page should load an exported INewsService and show the news on the page.

But there is the problem. I cant just use [Import] in the controllers, as they are just created when they are needed.

Edit: (Importing them to the main MVCApplication class doesn't work, becouse i cant access it from the controllers.) I think i found a way to access the main app via HttpContext.ApplicationInstance. But the Service object in this instance is null although it was created successfully in the Application_Start() method. Any idea why?

So, how can i access the NewsService from within a controller?

Thanks in advance, Marks

© Stack Overflow or respective owner

Related posts about mvc

Related posts about MEF