Dynamically load assemblies in ASP.NET 5

I had code that scanned the bin directory of my application for assemblies that had not yet been loaded into AppDomain, and downloaded them. It basically looked like this:

 foreach (var assemblyPath in Directory.GetFiles("path\to\bin", "*.dll")) { var inspected = Assembly.ReflectionOnlyLoadFrom(assemblyPath); Assembly.Load(inspected.GetName()); } 

I missed the try / catch clauses, etc. to be short.

This allowed me to remove assemblies in the bin folder at run time with implementations for specific interfaces and let the IoC container automatically pick them up. Now with the new Roslyn magic, there is no longer a physical DLL when debugging. Is there a way to get assembly names, project names, or dependency names (in project.json ) dynamically.

I think I need to implement something like this example in the Entropy repository , but I don’t know how to implement it for my scenario.

+5
source share
4 answers

What you are looking for is an ILibraryManager implementation that provides access to a complete dependency graph for an application. This is already flowing through the ASP.NET 5 DI system. So you can contact him from there.

A usage example can be found inside the RoslynCompilationService .

+2
source

You can use the IAssemblyLoadContextAccessor interface to dynamically load ASP.NET 5 class library projects (.xproj). The following code example works with beta 4:

 public class Startup { public void Configure(IApplicationBuilder app) { var assemblyLoadContextAccessor = app.ApplicationServices.GetService<IAssemblyLoadContextAccessor>(); var loadContext = assemblyLoadContextAccessor.Default; var loadedAssembly = loadContext.Load("NameOfYourLibrary"); } } 
+6
source

I solved this problem partially with ILibraryManager , as suggested by @tugberk. I changed the approach a bit, which eliminated the need to scan the bin folder for new builds. I just want all loaded assemblies in the current AppDomain.

I injected an instance of the ILibraryManager interface into my finder class type and used the GetReferencingLibraries() method with the name of the main assembly referenced by all other assemblies in the application.

An example implementation can be found here where this is an important part:

 public IEnumerable<Assembly> GetLoadedAssemblies() { return _libraryManager.GetReferencingLibraries(_coreAssemblyName.Name) .SelectMany(info => info.Assemblies) .Select(info => Assembly.Load(new AssemblyName(info.Name))); } 
+1
source

For basic .net users, here is my code to download assemblies from a specific path. I had to use directives as it is slightly different for the .Net Framework and .Net Core.

In the header of your class, you need to declare the use of something similar to:

 #if NET46 #else using System.Runtime.Loader; #endif 

And in your function, something similar to the following:

 string assemblyPath = "c:\temp\assmebly.dll"; #if NET46 Assembly assembly = Assembly.LoadFrom(assemblyPath); #else AssemblyLoadContext context = AssemblyLoadContext.Default; Assembly assembly = context.LoadFromAssemblyPath(assemblyPath); #endif 
0
source

All Articles