How to separate webapi controllers in their own app domain?

804 views Asked by At

I'm brainstorming some ideas about how to isolate web api controllers into their own "modules" that can be blended together into a single webapi application. However I'd like to isolate them and their dependencies form each other since there will likely be assembly version conflicts. I've been wondering if this could be achieved with different application domains for each controller. Anyone have experience with this?

1

There are 1 answers

0
ddoti On

Anything in the bin folder will be loaded into the current app domain so normally this isn't an issue. However, when dodging versioning conflicts you can use a custom assembly resolver to load up multiple assemblies/versions into the same AppDomain. This will allow you to pull in additional assemblies to web api's AppDomain.


    public class CustomAssemblyResolver : DefaultAssembliesResolver
    {
        public override ICollection GetAssemblies() 
        { 
             // load all of your assemblies and return 
        }
    }

You then will replace the default assembly resolver for web api in the configuration.


    config.Services.Replace(typeof(IAssembliesResolver), new CustomAssemblyResolver());

When web api goes to search assemblies to find controllers it will now pull in all of the additional assemblies. See this example for a more detailed explaination.