Andranik Sargsyan
Andranik Sargsyan

Reputation: 53

Load ASP.NET Web API plugin assemblies in separate AppDomain

I'm creating a CMS-like application under MVC.

To handle backend code for plugins I use Web API, so putting my API DLLs in /bin folder enables me to access plugin service methods by a specific route - /api/{namespace}/{controller}/{id}.

I would like to load the assemblies from another location, then be able to unload them in case of new module install/upgrade/delete.

People suggest to override the DefaultAssembliesResolver to include plugin assemblies:

public class PluginAssembliesResolver : DefaultAssembliesResolver
{
    public override ICollection<Assembly> GetAssemblies()
    {
        List<Assembly> assemblies = new List<Assembly>(base.GetAssemblies());

        foreach (string assemblyPath in ModuleManager.GetAllAssemblyPaths(HttpContext.Current.Server))
        {
            if (File.Exists(assemblyPath))
            {
                assemblies.Add(Assembly.LoadFrom(assemblyPath));
            }
        }

        return assemblies;
    }
}

But in this case I am loading the assemblies into the current domain and I am not able to unload them.

Trying to load assemblies from another location throws an error (it recognizes my assembly, but tries to look for the same DLL under /bin or /bin/myPlugin folders):

AppDomain domain = AppDomain.CreateDomain("myDomain", null);     

Assembly assembly = Assembly.LoadFrom(@"C:\myPluginPath\myPlugin.dll");

domain.Load(assembly.GetName());

How can I load them into a separate AppDomain?

Thank you.

Upvotes: 0

Views: 1455

Answers (2)

Nick
Nick

Reputation: 123

You need to overwrite the DefaultHttpControllerSelector. Here is a very nice article on the subject: link Hope this helps

Upvotes: 1

Raphael Fischer
Raphael Fischer

Reputation: 53

Try domain.Load(assembly.FullName()) Maybe this helps. I'm interested in a similar application. I'm trying to find a way of loading them in different Application Domains and make it work. I'm loading my DLLs via Microsoft Addin Framework which allows me to load addins into different appDomains for the purpose of unloading them on runtime. However I'm not sure if and how ASP WebAPI can handle multpile ApplicationDomains. Application Domains restrict the communication between the two DLLs. Are you using one ASP WebApi Server or do you create

Upvotes: 0

Related Questions