Transition from `IAssemblyLoadContext` to `AssemblyLoadContext`.

- `IAssemblyLoadContext` is a DNX defined type that will eventually go away.

#3365
This commit is contained in:
N. Taylor Mullen 2015-10-21 10:05:41 -07:00
parent 493a556168
commit faa78a3d91
2 changed files with 39 additions and 6 deletions

View File

@ -9,6 +9,9 @@ using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.PortableExecutable;
#if DOTNET5_4
using System.Runtime.Loader;
#endif
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Mvc.Razor.Internal;
using Microsoft.CodeAnalysis;
@ -32,12 +35,15 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
private readonly ILibraryExporter _libraryExporter;
private readonly IApplicationEnvironment _environment;
private readonly IAssemblyLoadContext _loader;
private readonly ICompilerOptionsProvider _compilerOptionsProvider;
private readonly IFileProvider _fileProvider;
private readonly Lazy<List<MetadataReference>> _applicationReferences;
private readonly string _classPrefix;
#if DOTNET5_4
private readonly RazorLoadContext _razorLoadContext;
#endif
/// <summary>
/// Initalizes a new instance of the <see cref="RoslynCompilationService"/> class.
/// </summary>
@ -52,19 +58,21 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</param>
public RoslynCompilationService(
IApplicationEnvironment environment,
IAssemblyLoadContextAccessor loaderAccessor,
ILibraryExporter libraryExporter,
ICompilerOptionsProvider compilerOptionsProvider,
IMvcRazorHost host,
IOptions<RazorViewEngineOptions> optionsAccessor)
{
_environment = environment;
_loader = loaderAccessor.GetLoadContext(typeof(RoslynCompilationService).GetTypeInfo().Assembly);
_libraryExporter = libraryExporter;
_applicationReferences = new Lazy<List<MetadataReference>>(GetApplicationReferences);
_compilerOptionsProvider = compilerOptionsProvider;
_fileProvider = optionsAccessor.Value.FileProvider;
_classPrefix = host.MainClassNamePrefix;
#if DOTNET5_4
_razorLoadContext = new RazorLoadContext();
#endif
}
/// <inheritdoc />
@ -131,11 +139,11 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
if (_supportsPdbGeneration.Value)
{
pdb.Seek(0, SeekOrigin.Begin);
assembly = _loader.LoadStream(ms, pdb);
assembly = LoadStream(ms, pdb);
}
else
{
assembly = _loader.LoadStream(ms, assemblySymbols: null);
assembly = LoadStream(ms, assemblySymbols: null);
}
var type = assembly.GetExportedTypes()
@ -146,6 +154,15 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
}
}
private Assembly LoadStream(MemoryStream ms, MemoryStream assemblySymbols)
{
#if NET451
return Assembly.Load(ms.ToArray(), assemblySymbols?.ToArray());
#else
return _razorLoadContext.Load(ms, assemblySymbols);
#endif
}
private CSharpCompilation Rewrite(CSharpCompilation compilation)
{
var rewrittenTrees = new List<SyntaxTree>();
@ -321,5 +338,20 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
return null;
}
#if DOTNET5_4
private class RazorLoadContext : AssemblyLoadContext
{
protected override Assembly Load(AssemblyName assemblyName)
{
return Default.LoadFromAssemblyName(assemblyName);
}
public Assembly Load(Stream assembly, Stream assemblySymbols)
{
return LoadFromStream(assembly, assemblySymbols);
}
}
#endif
}
}

View File

@ -41,6 +41,7 @@
"dotnet5.4": {
"dependencies": {
"System.Text.Encoding": "4.0.11-*",
"System.Runtime.Loader": "4.0.0-*",
"System.Threading.Tasks.Parallel": "4.0.1-beta-*"
}
}
@ -50,4 +51,4 @@
"node_modules",
"bower_components"
]
}
}