2

In my project in my main web assembly before I register routes in global.asax I load a number of external assemblies. Some of these assemblies have MVC Controllers and views inside of them.

My problem is that these external controllers are not picked up for some reason, I assume that controller registration happens somewhere before my assemblies are loaded, because if I reference my assemblies before application starts(dlls in bin folder) everything works fine.

So I'm wondering if there`s

  • a way to control when mvc controllers are registered, so I can do it after all my assemblies are loaded? OR
  • a way to inject my external controllers?

I had a similar issue with WebAPI controllers before and I was able to fix it by using my custom HttpControllerSelector, but I wasn't able to find anything similar in MVC yet.

Erik Philips
  • 51,408
  • 11
  • 123
  • 146
Maksim Vi.
  • 8,809
  • 12
  • 56
  • 85
  • Create your own controller factory? Use a DI container that can include multiple sources (most have this functionality in their configuration, e.g. `cfg.FromAssembly("My.Other.Asm")` – Brad Christie Jul 17 '13 at 19:03

2 Answers2

6

was able to solve the issue by overriding DefaultControllerFactory's CreateController method:

Application_Start:

IControllerFactory factory = new ControllerFactory();
ControllerBuilder.Current.SetControllerFactory(factory);

ControllerFactory:

public class ControllerFactory : DefaultControllerFactory
{
    public override IController CreateController(RequestContext requestContext, string controllerName)
    {
         IController controller = null;
         Type controllerType;
         if (ControllerTypes.TryGetValue(controllerName.ToLower(), out controllerType))
         {
              controller = (IController) Activator.CreateInstance(controllerType);
         }
         return controller;
    }
    ...
}

ControllerTypes is a dictionary of my MVC Controllers from external assemblies.

Maksim Vi.
  • 8,809
  • 12
  • 56
  • 85
1

See this and this - to create a custom controller factory using castle windsor etc.

turdus-merula
  • 7,958
  • 8
  • 33
  • 48
  • 1
    I'm going to take a look, but on a first sight it looks like an overkill for such a simple task. – Maksim Vi. Jul 17 '13 at 19:18
  • @MaksimVi.: overkill? Maybe for this task at face value, but you'll become a better developer if you get in to using DI. – Brad Christie Jul 17 '13 at 19:30
  • 1
    @BradChristie, getting into additional technologies/patterns/practices doesn't automatically make you a better developer. For this problem it looks like a silver bullet, but if you'd help me to understand how in my particular example I can benefit from IoC I might reconsider. – Maksim Vi. Jul 18 '13 at 22:57
  • @MaksimVi.: Not exposing yourself to other "technologies/patterns/practices" is like being a carpenter with only a hammer and a saw; sure you can get by, by the more tools in your belt the better chance you have at developing a solid product. IoC is not a silver bullet, you're right--but it does help with enforcing modular development and removal of dependencies. You also force yourself to get in to creating levels of abstractions instead of sticking with fixed implementations (which lends itself to unit testing and more robust solutions & less time mainaining in the end). – Brad Christie Jul 19 '13 at 12:29
  • 1
    @BradChristie What if I told you that there are more technologies out there than you can possibly learn? In case of IoC and DI you obviously know more than I do and as I said I'm going to take a look at it closely, but for the problem in my question (registering controls in MVC Framework environment) you still didn't tell me how I can benefit from using these methods. – Maksim Vi. Jul 19 '13 at 18:12
  • Don't get me wrong, overusing or not using a technology when you need it and aware of it existence is, of course, bad. The first case is the worst in my opinion, since it affects your design and getting rid of something is harder than adding to or optimizing your projects later, so I try to check twice before I start using something new. – Maksim Vi. Jul 19 '13 at 18:26
  • At the risk of not having enough room in a comment to explain, at sufficient detail, the benefits of DI and IoC: http://programmers.stackexchange.com/questions/19203/what-are-the-benefits-of-using-dependency-injection-and-ioc-containers & http://stackoverflow.com/questions/871405/why-do-i-need-an-ioc-container-as-opposed-to-straightforward-di-code – Brad Christie Jul 19 '13 at 18:34