stevo stevo - 15 days ago 6
ASP.NET (C#) Question

How to load assemblies in ASP.NET Core 1.0 RC2

migrating my web app from ASP.NET Core RC1 to RC2. I'm trying to load my referenced class libraries.

This code snippet doesn't work with RC2 anymore.

public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// libraryManager is null ....
ILibraryManager libraryManager = app.GetService<ILibraryManager>();

List<Assembly> result = new List<Assembly>();

IEnumerable<Library> libraries = libraryManager.GetLibraries();

IEnumerable<AssemblyName> assemblyNames = libraries.SelectMany(e => e.Assemblies).Distinct();
assemblyNames = Enumerable.Where(assemblyNames, e => e.Name.StartsWith("projectNamespace"));

foreach (AssemblyName assemblyName in assemblyNames)
{
Assembly assembly = Assembly.Load(assemblyName);
.
.
.
}
}
}


Our help would be greatly appreciated, Thanks Stefan

Answer

I found a solution. I'm using now DependencyContext instead of ILibraryManager

var loadableAssemblies = new List<Assembly>();

var deps = DependencyContext.Default;            
foreach (var compilationLibrary in deps.CompileLibraries)
{
    if (compilationLibrary.Name.Contains(projectNamespace))
    {
        var assembly = Assembly.Load(new AssemblyName(compilationLibrary.Name));
        loadableAssemblies.Add(assembly);
    }
}