c# - 使用带有动态加载 DLL 的 CaSTLe Windsor 解析 Controller

标签 c# asp.net-mvc dll castle-windsor

加载 Web 应用程序后,CaSTLe Windsor 找不到 Controller 。路径“”的 Controller 未找到或未实现 IController。当我查看内核(在 CustomControllerFactory 中)时,我发现所有 Controller 都已正确注册。

主 MVC 应用程序加载 3 个其他 DLL。当我们在 Visual Studio 中直接引用 DLL 并加载它正在运行的插件类型时。但是当动态加载它时,它说失败。当我请求 URL 时,传入 GetControllerInstance 的上下文是正确的,但 Type 参数为空。

我正在使用 Assembload.LoadFrom 加载程序集。然后我检索 Types foreach 模块,它是插件的子类。这导致我有 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" }
              );
    }

自定义 Controller 工厂:

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);
        }
    }
}

注册 Controller 。完成此操作后,我可以在 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, Controller 的解析失败了。为什么 CaSTLe Windsor 在请求 Controller 时找不到 Type?

最佳答案

这里的问题不是您的 Controller 的 Windsor 分辨率。 DefaultControllerType.GetControllerType() 方法可能返回 null,因为您没有将程序集添加到 BuildManager(使用 BuildManager .AddReferencedAssembly(程序集))。请记住,您只能在应用程序启动之前调用它,因此您需要使用 [assembly:PreApplicationStartupMethod(typeof(...SomeType), "PublicStaticVoidMethodOnSomeType")

关于c# - 使用带有动态加载 DLL 的 CaSTLe Windsor 解析 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35272788/

相关文章:

c# - 自定义错误页面

c# - 在运行时将接口(interface)的不同实现注入(inject)命令

asp.net-mvc - ASP.NET身份与简单成员资格的利与弊?

visual-studio-2010 - 如何编译使用 boost 库的 DLL?

java - 如何从 jna 或其他库访问 vb.net 原生 dll 文件的类和方法

c# - 在这种情况下如何使用异常?

c# - 禁用用户输入,直到 thread.sleep 完成

asp.net-mvc - 如何从自定义 ModelBinder 中调用 UpdateModel? (MVC)

c# - 如何在 AuthorizeAttribute ASP.NET Web.API MVC 5 的 IsAuthorized 上获取 Post 参数

c++ - 如何制作托管 (clr) 多线程 C++ .dll?