Simple Injector with asp.net mvc 4, load controller from another assembly

被刻印的时光 ゝ 提交于 2019-12-04 14:16:50

I'm afraid you'll have to do some more research, since your question is a bit vague at the moment, but here are some pointers:

Check if System.Web.Compilation.BuildManager.GetReferencedAssemblies() returns your plugin assemblies. The MVC DefaultControllerFactory searches for Controller types in all assemblies returned from that method. Best is to place your plugin folder inside the /bin directory. If I'm not mistaken GetReferencedAssemblies() also looks in sub directories of /bin. You'll probably need to write a custom controller factory if plugins must be loaded more dynamically.

Also take a look at this article, as it describes how to force the BuildManager to know about your plugin assemblies.

Your configuration seems overly complex. The following configuration should do the trick as well:

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's configuration to look what it contains. For instance, this will query all registered controllers:

var registeredControllerTypes = (
    from registration in container.GetCurrentRegistrations()
    where typeof(IController).IsAssignableFrom(registration.ServiceType)
    select registration.ServiceType)
    .ToArray();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!