How to detect controllers from other assemblies in web.api

If you need to detect web api controllers from outside the executing assembly, you can do so by replacing the default api IAssembliesResolver implementation with a custom class.

Create a class that implement default assemblies resolver and make your own implementation, here’s an example:

public class CustomAssembliesResolver : DefaultAssembliesResolver


    public List<Assembly> Assemblies { get; set; }

    public CustomAssembliesResolver(List<Assembly> assemblies)


        if (assemblies == null)

            throw new InvalidOperationException();

        Assemblies = assemblies;


    public override ICollection<Assembly> GetAssemblies()


        return Assemblies;



Now in your http configuration register class (WebApiConfig.Register)

Add the following line

config.Services.Replace(typeof(IAssembliesResolver), new CustomAssembliesResolver (new List&lt;Assembly&gt;() { typeof(MyOtherController).Assembly }));

In some situation, it can be useful to provide the runtime additional ways to resolve assemblies. There are two approach for this.

One way is to add .config file settings to specify additional paths for fusion to look at assemblies. This feature is called probing and you can read more about it here:

The other approach is to register an event handler on your app domain and provide the paths programmatically at runtime.

  1. Add a handler to this event
  2. AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolver.CurrentDomain_AssemblyResolve;
  3. Inside this handler you can use Assembly.LoadFrom(..) and return the assembly. Note that if you cannot resolve the assembly, simply return null will fall back to fusion load process.

Important to note that if you are loading assemblies from file, I strongly recommend using LoadFrom method instead of Load, as Load from will not be aware of the version you are loading and could lead to performance and stability issues at runtime.

Submit a Comment

Your email address will not be published. Required fields are marked *

Share This