9

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. 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.

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.

May be there is an option to "re-load" all controllers.

Any help will be appreciated!

Marius

2
  • A former colleague of mine got something almost identical to this working in our system. I know he ended up using MEF to manage dynamic assembly loading, but I don't remember the specifics. I sent him a link to this post, perhaps he'll get back to you. In the meantime, you can try coming up with a solution yourself with MEF. Commented Nov 27, 2012 at 8:47
  • dotnetcurry.com/ShowArticle.aspx?ID=898 have you seen this. Commented Jun 25, 2013 at 13:37

5 Answers 5

2

You could use Web API Dependency Resolver:

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

Sign up to request clarification or add additional context in comments.

Comments

2

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(...).

In oder to do a type-cache refresh, I have to implement a custom HttpControllerSelector.

Comments

2

MEF is the way to go. 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

Edit: dead link, see Wayback https://web.archive.org/web/20130619191423/http://www.fidelitydesign.net/?p=104

Comments

0

I think you need take a look at this http://haacked.com/archive/2010/01/17/editable-routes.aspx as a start point.

Comments

0

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

void RegisterControllers(RouteCollection routes);

Create an instance and then pass in your route collection. This would then register the additional routes during the website startup.

2 Comments

Hi Peter, I want to dynamically loading additional WebAPi controllers at a later time (when the web site is already running). I know how to load controllers in global.asax, but this happens just one time - on start-up.
Doesn't IIS restart the web app anyway when you alter the contents of the binaries folder?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.