Why are my controllers being instantiated when they're not being called?

前端 未结 2 1794
我寻月下人不归
我寻月下人不归 2021-01-17 00:38

Here are the symptoms I am experiencing:

I have a brand new empty controller in an area:

public class JamController : Controller
{
    public JamCo         


        
2条回答
  •  执念已碎
    2021-01-17 01:39

    This is down to a bug in MefContrib.Web.Mvc. This assembly implements it's own ControllerFactory that inherits from DefaultControllerFactory.

    The factory overrides GetControllerType, I assume to try and resolve controllers that live in assemblies somewhere other than the default application or it's references. The implementation of GetControllerType first calls into base.GetControllerType to see if Defaultcontroller can resolve it.

    If it can't - which is the case for urls that don't exist - it asks MEF for all exports that implement IController. This returns an IEnumerable> with one item for every class that implements IController in the bin/ folder (by default).

    It then runs a linq query over the IEnumerable, calling GetType() on the Value property of each Lazy. Requesting the Value of a Lazy forces the instance to be created. This is why every controller in the bin/ is being constructed for a page that doesn't exist.

    I don't think this is an easy problem to fix properly as there is no way of getting the Type instance from Lazy.Value without creating the value. However by removing the lines from AppStart_MefContribMVC3.cs that register the ControllerFactory with Asp.Net - you have effectively stopped using MefContrib.Web.Mvc's ControllerFactory and just used Asp.Net's DefaultControllerFactory instead.

提交回复
热议问题