Simple injector with asp.net mvc 4, load controller from different assembly

I am developing asp.net mvc 4 site using Simple Injector as Ioc tool. It would be a pluggable architecture. Some controllers and views are in a different assembly (another mvc4 application, Plugin.Web.dll). And from the main application, I know the path to Plugin.Web.dll by loading the plugin.

container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

container.RegisterMvcAttributeFilterProvider();

container.Options.AllowOverridingRegistrations = true;

var appPath = AppDomain.CurrentDomain.BaseDirectory;

string[] files = Directory.GetFiles(appPath + "\\Plugins", "*",
    SearchOption.AllDirectories);

foreach (var fileName in files)
{
    Console.WriteLine(fileName);

    var assembly = Assembly.LoadFrom(fileName);

    container.RegisterMvcControllers(assembly);

    var controllerTypes =
        from type in assembly.GetExportedTypes()
        where type.Name.EndsWith("Controller",
            StringComparison.Ordinal)
        where typeof(IController).IsAssignableFrom(type)
        where !type.IsAbstract
        select type;

    // Instead of verify:
    foreach (var type in controllerTypes)
    {
        container.GetInstance(type);
    }
}            

container.Options.AllowOverridingRegistrations = false;       

container.Verify();

DependencyResolver.SetResolver(
    new SimpleInjectorDependencyResolver(container));

      

He gives no errors.

But when I press this click, in sight:

@Html.ActionLink("plugin page","PluginPage","Plugin")

      

It gives "The resource could not be found., Http 404 '

early

+3


source to share


1 answer


I'm afraid you will have to do some more research as your question is a little vague right now, but here are some pointers:

Check if System.Web.Compilation.BuildManager.GetReferencedAssemblies()

your builds are returning plugins. MVC DefaultControllerFactory

searches for types Controller

in all assemblies returned by this method. Your best bet is to place your plugins folder in the / bin directory. If I'm not mistaken, GetReferencedAssemblies()

also viewed in the / bin subdirectories. You will probably need to write a custom factory controller if plugins need to load more dynamically.

Also consider this article as it describes how to make BuildManager know about your plugin builds.

Your configuration seems too complicated. The following setup should do the trick too:



container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

container.RegisterMvcAttributeFilterProvider();

var appPath = AppDomain.CurrentDomain.BaseDirectory;

string[] files = Directory.GetFiles(appPath + "\\bin\\Plugins", "*",
    SearchOption.AllDirectories);

container.RegisterMvcControllers(
    from fileName in files
    select Assembly.LoadFrom(fileName)
);    

container.Verify();

      

You can query the container configuration to see what it contains. For example, this will query all registered controllers:

var registeredControllerTypes = (
    from registration in container.GetCurrentRegistrations()
    where typeof(IController).IsAssignableFrom(registration.ServiceType)
    select registration.ServiceType)
    .ToArray();

      

+4


source







All Articles