ronaldm February 2016

Resolving controllers using Castle Windsor with dynamically loaded DLLs

After loading the web application, Castle Windsor cannot find the controllers. The controller for path '' was not found or does not implement IController. When I look into the kernel (in the CustomControllerFactory) I see that all the controllers are correctly registered.

The main MVC application loads 3 other DLL's. When we directly reference the DLL's in Visual studio and load the Plugin types it is working. But when dynamically loading it, it says fails. When I request a URL the context being passed into the GetControllerInstance is correct but the Type parameter is null.

I am loading an assembly using Assembload.LoadFrom.Then I retrieve the Types foreach module, which is a subclass of Plugin. Which results in the 3 types I have.

Assembly assembly = Assembly.LoadFrom(module);

Type pluginType = assembly.GetTypes()
                    .Single(x => x.IsSubclassOf(typeof(Plugin)));

Then I create an instance of the Plugin, which I use to register the routes.

(IPlugin)Activator.CreateInstance(type))

RegisterRoutes:

    public static void RegisterRoutes(RouteCollection routes, IEnumerable<IPlugin> plugins)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        var pluginRouteDefaults = new {action = "Index", id = UrlParameter.Optional};
        foreach (var plugin in plugins)
        {
            var context = new AreaRegistrationContext(plugin.Area, routes);
            context.MapRoute(plugin.Area, $"{plugin.Area}/{{controller}}/{{action}}/{{id}}", pluginRouteDefaults, plugin.GetControllerNamespaces().ToArray());
        }

        routes.MapRoute(
              name: "Default",
              url: "{area}/{controller}/{action}/{id}",
              defaults: new { area = "Framework", controller = "Home", action = "Index", id = UrlParameter.Optional },
              namespaces: new string[] { "Web.Framework.Controllers" }
              );
         

Answers


mycroes February 2016

The problem here is not the Windsor resolution of your controllers. The DefaultControllerType.GetControllerType() method is probably returning null, because you didn't add the assemblies to the BuildManager (with BuildManager.AddReferencedAssembly(assembly)). Keep in mind you can only call this before application start, so you need to use [assembly:PreApplicationStartupMethod(typeof(...SomeType), "PublicStaticVoidMethodOnSomeType").

Post Status

Asked in February 2016
Viewed 1,868 times
Voted 13
Answered 1 times

Search




Leave an answer