使用带有动态加载 DLL 的 Castle Windsor 解析控制器
Resolving controllers using Castle Windsor with dynamically loaded DLLs
加载 Web 应用程序后,Castle Windsor 找不到控制器。路径“”的控制器未找到或未实现 IController。当我查看内核(在 CustomControllerFactory 中)时,我发现所有控制器都已正确注册。
主 MVC 应用程序加载其他 3 个 DLL。当我们直接引用 Visual studio 中的 DLL 并加载它工作的插件类型时。但是当动态加载它时,它说失败。当我请求 URL 时,传入 GetControllerInstance 的上下文是正确的,但 Type 参数为空。
我正在使用 Assembload.LoadFrom.Then 加载程序集 我检索了 Types foreach 模块,它是插件的子class。这导致我有 3 种类型。
Assembly assembly = Assembly.LoadFrom(module);
Type pluginType = assembly.GetTypes()
.Single(x => x.IsSubclassOf(typeof(Plugin)));
然后我创建一个插件实例,我用它来注册路由。
(IPlugin)Activator.CreateInstance(type))
注册路线:
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" }
);
}
自定义控制器工厂:
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 中的模块 window 中加载了 DLL。 AppDomain.CurrentDomain.GetAssemblies() 也表示 DLL 已加载。
container.Register(
Classes.FromThisAssembly().BasedOn<IController>().LifestyleTransient());
MvcApplication class 我在其中找到 Dll。
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,控制器的解析失败了。为什么温莎城堡在请求控制器时找不到类型?
这里的问题不是你的控制器的温莎分辨率。 DefaultControllerType.GetControllerType()
方法可能返回 null
,因为您没有将程序集添加到 BuildManager
(使用 BuildManager.AddReferencedAssembly(assembly)
)。请记住,您只能在应用程序启动之前调用它,因此您需要使用 [assembly:PreApplicationStartupMethod(typeof(...SomeType), "PublicStaticVoidMethodOnSomeType")
.
加载 Web 应用程序后,Castle Windsor 找不到控制器。路径“”的控制器未找到或未实现 IController。当我查看内核(在 CustomControllerFactory 中)时,我发现所有控制器都已正确注册。
主 MVC 应用程序加载其他 3 个 DLL。当我们直接引用 Visual studio 中的 DLL 并加载它工作的插件类型时。但是当动态加载它时,它说失败。当我请求 URL 时,传入 GetControllerInstance 的上下文是正确的,但 Type 参数为空。
我正在使用 Assembload.LoadFrom.Then 加载程序集 我检索了 Types foreach 模块,它是插件的子class。这导致我有 3 种类型。
Assembly assembly = Assembly.LoadFrom(module);
Type pluginType = assembly.GetTypes()
.Single(x => x.IsSubclassOf(typeof(Plugin)));
然后我创建一个插件实例,我用它来注册路由。
(IPlugin)Activator.CreateInstance(type))
注册路线:
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" }
);
}
自定义控制器工厂:
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 中的模块 window 中加载了 DLL。 AppDomain.CurrentDomain.GetAssemblies() 也表示 DLL 已加载。
container.Register(
Classes.FromThisAssembly().BasedOn<IController>().LifestyleTransient());
MvcApplication class 我在其中找到 Dll。
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,控制器的解析失败了。为什么温莎城堡在请求控制器时找不到类型?
这里的问题不是你的控制器的温莎分辨率。 DefaultControllerType.GetControllerType()
方法可能返回 null
,因为您没有将程序集添加到 BuildManager
(使用 BuildManager.AddReferencedAssembly(assembly)
)。请记住,您只能在应用程序启动之前调用它,因此您需要使用 [assembly:PreApplicationStartupMethod(typeof(...SomeType), "PublicStaticVoidMethodOnSomeType")
.