Web API 2 Is it possible to programmatically load a route / controller?

I am currently working on a corporate web application that uses WCF to implement the REST API. It uses VirtualPathProvider to search for requests to * .svc files (which actually do not exist), and then creates 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 affecting the web server or anyone who uses it.

What I would like to know is that with the web interface API 2 is conceptually possible. Iโ€™m doing some research, but it seems that routes can only be set up at startup ... What I was hoping is a tool for handling non-existent routes and basically uses the name of the controller from the request to search and load the associated assembly (if it exists) when programmatically adding to her new route.

I just started with Web API 2, so I was hoping some more experienced users would be able to listen. My team is mainly interested in migrating to Web API 2 in order to reduce the overhead and complexity that we faced with WCF, but this particular requirement may be a deal breaker.

+8
asp.net-web-api programmatically-created asp.net-web-api-routing
source share
1 answer

Well, therefore, after much research ... I tracked the correct class to override it, and now you can request a check to see if the controller was allowed, and if not, try loading the appropriate assembly into memory (based on the name of the controller at the moment) and return the associated controller.

Here is the code:

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

and, of course, you will need to replace the service in the WebApiConfig registry method file as follows:

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

There is some work to be done, but this is a good start. This allows me to dynamically add controllers to the hosting site during its operation and without the need to disconnect.

The main problem with this code, obviously, is that a recently loaded controller is not added to the list of registered controllers, so an exception is always raised and handled with every request (for these controllers). I am studying whether I can add it to the registered list in any way, so we will see where it leads from.

+9
source share

All Articles