Search code examples
c#asp.net.netcoreclr

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);
            .
            .
            .
        }
    }
}

Solution

  • 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);
        }
    }