MEF2 with ChildAction

Feb 5, 2012 at 10:05 AM

I use @Html.Action(..,..) on the _Layout.cshtml to render an action sensitive menu (it's model depends on he action and its parameters). It worked before I added the Mef2 references.

I added the Mef2 references, made the Parts folder and the classes in a right named namespace. Added the constructor to my Controller class. The child action is in the CatalogController and this controller needs a resolved part.

Now it works nice with any actions of any Controller but the CatalogController. If I use an url that tarets the same controller (for this case: localhost:3456/Catalog/Index) what the Html.Action then I get the error:

A single instance of controller 'Shop.Web.Controllers.CatalogController' cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.

and it happens on the Html.Action line

Line 25:         </header>
Line 26:         <nav id="sidemenu">
Line 27:         @Html.Action("Menu","Catalog")
Line 28:         </nav>
Line 29:         <section id="main">

So it seems I can not use child actions in the same controller where the actual main action resides.
Is it by design or I missed something? Can I make it work somehow? 

Feb 5, 2012 at 3:59 PM

Hmm, I can not use child actions in a sensible way at all. All the child actions must be in different controllers to avoid this error message with mef2. I am ceartainly missed something because it's crazy.

Feb 5, 2012 at 4:09 PM

In the FabrikamBooks example:

In the HomeController write a new action method:

public ActionResult List()
{
    var books = _dbContext.Set<Book>()
        .OrderBy(b => b.Title)
        .Select(b => new BookModel { Id = b.Id, Title = b.Title })
        .ToArray();
 
    return View(books);
}

Creatre a view for it (sorry you won't miss the view becauseof the error :( )

At the end of the Home/Index.cshtml write a single line

@Html.Action("List")
Run it. You'll get the error above. 
Feb 6, 2012 at 5:51 PM

Thanks – this sounds like a bug in the composition provider, we’ll move it to the tracker and follow up as soon as possible.

From: tocsi [email removed]
Sent: Sunday, February 05, 2012 9:10 AM
To: Nicholas Blumhardt
Subject: Re: MEF2 with ChildAction [MEF:293918]

From: tocsi

In the FabrikamBooks example:

In the HomeController write a new action method:

public ActionResult List()
{
    var books = _dbContext.Set<Book>()
        .OrderBy(b => b.Title)
        .Select(b => new BookModel { Id = b.Id, Title = b.Title })
        .ToArray();
 
    return View(books);
}

Creatre a view for it (sorry you won't miss the view becauseof the error :( )

At the end of the Home/Index.cshtml write a single line

@Html.Action("List")
Run it. You'll get the error above.