Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load assemblies in ASP.NET Core 1.0 RC2

I am 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 any more:

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);
            .
            .
            .
        }
    }
}
like image 839
stevo Avatar asked May 19 '16 06:05

stevo


1 Answers

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);
    }
}
like image 66
stevo Avatar answered Sep 19 '22 11:09

stevo