Web API 2是否可以通过编程方式加载路径/控制器? [英] Web API 2 Is it possible to load a route/controller programmatically?

查看:74
本文介绍了Web API 2是否可以通过编程方式加载路径/控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用WCF实现REST API的企业Web应用程序上工作.它利用VirtualPathProvider捕获对* .svc文件(实际上不存在)的请求,然后动态构建请求以动态加载关联的WCF服务.这使系统具有可以在运行时添加到应用程序中的模块",而不会影响Web服务器或使用它的任何人.

I am currently working on an enterprise web application that uses WCF to implement a REST API. It utilizes a VirtualPathProvider to catch requests to *.svc files (which don't actually exist), and then builds them on the fly to dynamically load the associated WCF services. This allows the system to have "modules" that can be added to the application at runtime without impacting the web server or anyone using it.

我想知道的是,从概念上讲,是否可以使用Web API2.我一直在做一些研究,但是看来这些路由只能在启动时进行配置...我希望是处理不存在的路由的一种方法,基本上是使用请求中的控制器名称来查找并加载关联的程序集(如果存在),同时以编程方式向其中添加新的路由.

What I would like to know, is if the same is conceptually possible with Web API 2. I've been doing some research, but it looks like the routes can only be configured at startup... What I was hoping for is a means to handle for non-existent routes, and basically use the controller name from the request to look-up and load the associated assembly (if it exists) while programmatically adding a new route to it.

我刚开始使用Web API 2,所以我希望有更多有经验的用户加入.基本上,我的团队对切换到Web API 2感兴趣,以减少我们在WCF中遇到的开销和复杂性,但是这项特殊要求可能会破坏交易.

I've just started with Web API 2 so I was hoping for some more experienced users to chime in. Basically my team is interested in switching to Web API 2 to reduce the overhead and complexity we've encountered with WCF, but this particular requirement could be a deal breaker.

推荐答案

好吧,经过大量研究……我已经找到了要重写的适当类,现在可以按请求检查控制器是否能够要解决的问题,如果没有解决,请尝试将适当的程序集加载到内存中(基于当前的控制器名称),然后返回关联的控制器.

Okay, so after much research... I have tracked down the proper class to override, and can now per-request check whether or not the controller was able to be resolved, and if not, attempt to load the proper assembly into memory (based on controller name at the moment), and return the associated controller.

这是代码:

public class CustomHttpControllerSelector : DefaultHttpControllerSelector {
  private readonly HttpConfiguration _configuration;

  public CustomHttpControllerSelector(HttpConfiguration configuration) : base(configuration) {
    _configuration = configuration;
  }

  public override HttpControllerDescriptor SelectController(HttpRequestMessage request) {
    HttpControllerDescriptor controller;
    try {
      controller = base.SelectController(request);
    }
    catch (Exception ex) {
      String controllerName = base.GetControllerName(request);
      Assembly assembly = Assembly.LoadFile(String.Format("{0}pak\\{1}.dll", HostingEnvironment.ApplicationPhysicalPath, controllerName));
      Type controllerType = assembly.GetTypes()
        .Where(i => typeof(IHttpController).IsAssignableFrom(i))
        .FirstOrDefault(i => i.Name.ToLower() == controllerName.ToLower() + "controller");
      controller = new HttpControllerDescriptor(_configuration, controllerName, controllerType);
    }
    return controller;
  }
}

当然,您需要这样替换WebApiConfig的Register方法文件中的服务:

and of course you'd need to replace the service in the WebApiConfig's Register method file as such:

config.Services.Replace(typeof(IHttpControllerSelector), new CustomHttpControllerSelector(config));

这里肯定有更多工作要做,但这是一个好的开始.它允许我在启动和运行主机库时动态地向其添加控制器,而无需停机.

There is definitely more work to be done here, but this is a good start. It allows me to dynamically add controllers to the hosting website while it's up and running, without requiring an outage.

此代码的主要问题显然是新加载的控制器未添加到已注册控制器的列表中,因此总是针对每个请求(对于那些控制器)抛出并处理该异常.我正在研究是否可以通过某种方式将其添加到注册列表中,因此我们将看到结果.

The main issue with this code obviously is that the newly loaded controller isn't added to the list of registered controllers, so the exception is always thrown and handled on every request (for those controllers). I'm looking into whether or not I can add it to the registered list in some way, so we'll see where this leads.

这篇关于Web API 2是否可以通过编程方式加载路径/控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆