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.
Xorcist
source share