简体   繁体   English

ASP.Net WebApi:如何在运行时加载其他控制器

[英]ASP.Net WebApi: How to load additional controllers at runtime

in my Asp.Net MVC 4 WebApi application I want to load additional WebApiControllers dynamically at a later time (after the WebApi initialization), which are in separate assemblies. 在我的Asp.Net MVC 4 WebApi应用程序中,我想稍后(在WebApi初始化之后)动态加载其他WebApiControllers,它们位于不同的程序集中。 Furthermore I want to add routes for those controllers at runtime. 此外,我想在运行时为这些控制器添加路由。

I am wonder, if this is possible to do. 我很奇怪,如果可以的话。

My goal is to build a web-app, where I can upload controllers (compiled assemblies) and the controllers will be automatically hosted within this application. 我的目标是构建一个web应用程序,我可以在其中上传控制器(已编译的程序集),控制器将自动托管在此应用程序中。

I've already tried to achieve that by implementing my own AssemblyResolver class, but (as far as I have seen), the AssemblyResolver is loaded once at initialization phase. 我已经尝试通过实现我自己的AssemblyResolver类来实现这一点,但是(据我所见),AssemblyResolver在初始化阶段加载一次。

May be there is an option to "re-load" all controllers. 可能有一个“重新加载”所有控制器的选项。

Any help will be appreciated! 任何帮助将不胜感激!

Marius 马吕斯

You could use Web API Dependency Resolver: 您可以使用Web API依赖性解析器:

public class WebApiApplication : System.Web.HttpApplication
{
    void ConfigureApi(HttpConfiguration config)
    {
        config.DependencyResolver = new MyDependencyResolver();
    }

    protected void Application_Start()
    {
        ConfigureApi(GlobalConfiguration.Configuration);

        // ...
    }
}

Using the Web API Dependency Resolver 使用Web API依赖性解析程序

Thanks for your answers. 谢谢你的回答。

I figured it out, it is not possible to do that, since all of the controllers are loaded once and are cached all over the time. 我想通了,不可能这样做,因为所有的控制器都被加载一次并且在整个时间都被缓存。

See HttpControllerTypeCache in DefaultHttpControllerSelector method InitializeControllerInfoCache(...). 请参阅DefaultHttpControllerSelector方法InitializeControllerInfoCache(...)中的HttpControllerTypeCache。

In oder to do a type-cache refresh, I have to implement a custom HttpControllerSelector. 在oder中进行类型缓存刷新,我必须实现一个自定义的HttpControllerSelector。

MEF is the way to go. MEF是要走的路。 You will need to create your own controller factory to replace the default controller factory. 您需要创建自己的控制器工厂来替换默认的控制器工厂。 Take a look at this link http://www.fidelitydesign.net/?p=104 请看这个链接 http://www.fidelitydesign.net/?p=104

Edit: dead link, see Wayback https://web.archive.org/web/20130619191423/http://www.fidelitydesign.net/?p=104 编辑:死链接,请参阅Wayback https://web.archive.org/web/20130619191423/http://www.fidelitydesign.net/?p=104

我想你需要看一下这个http://haacked.com/archive/2010/01/17/editable-routes.aspx作为起点。

What about if in Global.asax you loaded all assemblies in a specific folder and then looked for a class that implements a specific interface, for example 如果在Global.asax中,您将所有程序集加载到特定文件夹中,然后查找实现特定接口的类,例如

void RegisterControllers(RouteCollection routes); void RegisterControllers(RouteCollection路由);

Create an instance and then pass in your route collection. 创建一个实例,然后传入您的路由集合。 This would then register the additional routes during the website startup. 然后,这将在网站启动期间注册其他路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC和ASP.NET WebAPI的负载平衡 - Load balancing for ASP.NET MVC and ASP.NET WebAPI 在ASP.NET WebAPI中的不同文件夹中调用控制器 - Calling controllers in different folders in ASP.NET WebAPI asp.net WebAPI两个具有相同路由但动词不同的控制器 - asp.net WebAPI Two controllers with same routes but different verbs 来自另一个项目的 asp.net webapi selfhost 控制器 - asp.net webapi selfhost controllers from another project ASP.NET WebApi-具有类似操作方法的两个控制器 - ASP.NET WebApi - Two Controllers With Similar Action Methods 如何在测试WebApi控制器时生成Asp.net用户身份 - How to generate Asp.net User identity when testing WebApi controllers 使用ASP.NET WebApi,如何使路由排除一组控制器? - Using ASP.NET WebApi, how can I make a route exclude a set of controllers? 如何在UWP(Windows Runtime)开发的应用程序中使用ASP.NET WebAPI服务(内置于.NET Framework中)? - How to consume ASP.NET WebAPI service (built in .NET Framework) in an application developed in UWP (Windows Runtime)? 如何在XML序列化运行时(ASP.Net WebApi)期间更改XML元素的名称 - How to change the name of an XML element during XML Serialization runtime (ASP.Net WebApi) 如何启动 webApi Asp.net - How to start a webApi Asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM