解析使用动态加载的DLL的Castle Windsor的控制器
作者:互联网
加载Web应用程序后,Castle Windsor无法找到控制器.找不到路径“”的控制器或未实现IController.当我查看内核时(在CustomControllerFactory中),我看到所有控制器都已正确注册.
MVC主应用程序加载其他3个DLL.当我们在Visual Studio中直接引用DLL并加载插件类型时,它就起作用了.但是,当动态加载它时,它会失败.当我请求URL时,传递到GetControllerInstance中的上下文是正确的,但Type参数为null.
我使用Assembload.LoadFrom加载程序集,然后检索Types foreach模块,该模块是Plugin的子类.结果是我有3种类型.
Assembly assembly = Assembly.LoadFrom(module);
Type pluginType = assembly.GetTypes()
.Single(x => x.IsSubclassOf(typeof(Plugin)));
然后,我创建一个插件实例,用于注册路由.
(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" }
);
}
CustomControllerFactory:
public class CustomControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel;
public VlcControllerFactory(IKernel kernel)
{
this._kernel = kernel;
}
public override void ReleaseController(IController controller)
{
_kernel.ReleaseComponent(controller);
}
protected override IController GetControllerInstance(RequestContext context, Type controllerType)
{
if (controllerType == null)
{
return base.GetControllerInstance(context, controllerType);
}
try
{
return (IController)_kernel.Resolve(controllerType);
}
catch
{
return base.GetControllerInstance(context, controllerType);
}
}
}
注册控制器.完成此操作后,我可以看到在Visual Studio的“模块”窗口中已加载了DLL.另外,AppDomain.CurrentDomain.GetAssemblies()表示已加载DLL.
container.Register(
Classes.FromThisAssembly().BasedOn<IController>().LifestyleTransient());
我在其中找到Dll的MvcApplication类.
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var directories = Directory.GetDirectories("C:\Projects\main\modules").Where(x => !x.EndsWith("Framework"));
string subPath = GetSubPath();
List<Type> pluginTypes = GetPluginTypes(directories, subPath);
var plugins = GetIPlugins(pluginTypes);
Launcher.CreateWindsorContainer(plugins.ToArray());
}
private static List<IPlugin> GetIPlugins(List<Type> pluginTypes)
{
List<IPlugin> plugins = new List<IPlugin>{new MvcInstaller()};
pluginTypes.ForEach(type => plugins.Add((IPlugin) Activator.CreateInstance(type)));
return plugins;
}
private static List<Type> GetPluginTypes(IEnumerable<string> directories, string subPath)
{
List<Type> pluginTypes = new List<Type>();
foreach (string directory in directories)
{
string module = Directory.GetFiles(directory + subPath).SingleOrDefault(x => x.EndsWith("Plugin.dll"));
if (!string.IsNullOrEmpty(module))
{
Assembly assembly = Assembly.LoadFrom(module);
Type pluginType = assembly.GetTypes()
.Single(x => x.IsSubclassOf(typeof(Plugin)));
pluginTypes.Add(pluginType);
}
}
return pluginTypes;
}
private static string GetSubPath()
{
#if DEBUG
var subPath = @"\bin\Debug\";
#else
subPath = @"\bin\Release\";
#endif
return subPath;
}
}
当我省略此代码并直接引用其他Dll时,请执行以下操作:
Launcher.CreateWindsorContainer(new PluginA(), new PluginB(), new MVCPlugin());
然后,它可以完美运行,但是由于加载了Dll,控制器解析失败.为什么在请求控制器时Castle Windsor无法找到类型?
解决方法:
这里的问题不是控制器的Windsor分辨率. DefaultControllerType.GetControllerType()方法可能返回null,因为您没有将程序集添加到BuildManager(使用BuildManager.AddReferencedAssembly(assembly)).请记住,只能在应用程序启动之前调用此函数,因此您需要使用[assembly:PreApplicationStartupMethod(typeof(… SomeType),“ PublicStaticVoidMethodOnSomeType”).
标签:dll,castle-windsor,c,asp-net-mvc 来源: https://codeday.me/bug/20191119/2033244.html