views:

363

answers:

1

I'm using ASP.NET MVC (1.0) and StructureMap (2.5.3), I'm doing a plugin feature where dll's with controller are to be picked up in a folder. I register the controllers with SM (I am able to pick it up afterwards, so I know it's in there)

foreach (string file in path)
{
    var assy = System.Reflection.Assembly.LoadFile(file);
    Scan(x =>{
      x.Assembly(assy);
      x.AddAllTypesOf<IController>();
    });
}

My problem is with the GetControllerInstance method of my override of DefaultControllerFactory. Everytime I send in enything else than a valid controller (valid in the sense that it is a part of the web project) I get the input Type parameter as null.

I've tried setting up specific routes for it.

I've done a test with Castle.Windsor and there it is not a problem.

Can anyone point me in the right direction? I'd appreciate it.

[Edit]

Here is the code:

-> Controller factory for Windsor

public WindsorControllerFactory()
{
  container = new WindsorContainer(new XmlInterpreter(
    new ConfigResource("castle")));
  // Register all the controller types as transient
  // This is for the regular controllers
  var controllerTypes = 
    from t in 
      Assembly.GetExecutingAssembly().GetTypes()
    where typeof(IController).IsAssignableFrom(t)
    select t;
  foreach (Type t in controllerTypes)
  {
    container.AddComponentLifeStyle(t.FullName, t,
      LifestyleType.Transient);
  }
  /* Now the plugin controllers */
  foreach (string file in Plugins() )
  {
    var assy = System.Reflection.Assembly.LoadFile(file);
    var pluginContr = 
      from t in assy.GetTypes() 
      where typeof(IController).IsAssignableFrom(t)
      select t;
    foreach (Type t in pluginContr)
    {
      AddToPlugins(t);
      /* This is the only thing I do, with regards to Windsor,
         for the plugin Controllers */
      container.AddComponentLifeStyle(t.FullName, t, 
         LifestyleType.Transient);
    }
  }
}

-> StructureMap; adding the controllers:

public class PluginRegistry : Registry
{
  public PluginRegistry()
  {
    foreach (string file in Plugins() ) // Plugins return string[] of assemblies in the plugin folder
    {
      var assy = System.Reflection.Assembly.LoadFile(file);
      Scan(x =>
      {
        x.Assembly(assy);
        //x.AddAllTypesOf<IController>().
        //     NameBy(type => type.Name.Replace("Controller", ""));
        x.AddAllTypesOf<IController>();
     });
    }
  }
}

-> Controller factory for SM version Not really doing much, as I'm registering the controllers with SM in the earlier step

public SMControllerFactory()
  : base()
{
  foreach (string file in Plugins() )
  {
    var assy = System.Reflection.Assembly.LoadFile(file);
    var pluginContr = 
      from t in assy.GetTypes()
      where typeof(IController).IsAssignableFrom(t)
  select t;
    foreach (Type t in pluginContr)
    {
      AddPlugin();
    }
  }
}
A: 

Can you post your controller factory?

I don't understand why Castle would work since I would think you would also get null passed in for the Type param of GetControllerInstance regardless of the DI framework you use inside that method. MVC is in charge of matching up the string name of the controller in the URL with a real type (unless you overrode those methods too). So I'm guessing it isn't the DI framework, but that MVC can't find your controller classes for some reason.

jayrdub