Transition from `IAssemblyLoadContext` to `AssemblyLoadContext`.
- `IAssemblyLoadContext` is a DNX defined type that will eventually go away. #3365
This commit is contained in:
parent
493a556168
commit
faa78a3d91
|
|
@ -9,6 +9,9 @@ using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Reflection.PortableExecutable;
|
using System.Reflection.PortableExecutable;
|
||||||
|
#if DOTNET5_4
|
||||||
|
using System.Runtime.Loader;
|
||||||
|
#endif
|
||||||
using Microsoft.AspNet.FileProviders;
|
using Microsoft.AspNet.FileProviders;
|
||||||
using Microsoft.AspNet.Mvc.Razor.Internal;
|
using Microsoft.AspNet.Mvc.Razor.Internal;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
@ -32,12 +35,15 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
|
|
||||||
private readonly ILibraryExporter _libraryExporter;
|
private readonly ILibraryExporter _libraryExporter;
|
||||||
private readonly IApplicationEnvironment _environment;
|
private readonly IApplicationEnvironment _environment;
|
||||||
private readonly IAssemblyLoadContext _loader;
|
|
||||||
private readonly ICompilerOptionsProvider _compilerOptionsProvider;
|
private readonly ICompilerOptionsProvider _compilerOptionsProvider;
|
||||||
private readonly IFileProvider _fileProvider;
|
private readonly IFileProvider _fileProvider;
|
||||||
private readonly Lazy<List<MetadataReference>> _applicationReferences;
|
private readonly Lazy<List<MetadataReference>> _applicationReferences;
|
||||||
private readonly string _classPrefix;
|
private readonly string _classPrefix;
|
||||||
|
|
||||||
|
#if DOTNET5_4
|
||||||
|
private readonly RazorLoadContext _razorLoadContext;
|
||||||
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initalizes a new instance of the <see cref="RoslynCompilationService"/> class.
|
/// Initalizes a new instance of the <see cref="RoslynCompilationService"/> class.
|
||||||
/// </summary>
|
/// </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>
|
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</param>
|
||||||
public RoslynCompilationService(
|
public RoslynCompilationService(
|
||||||
IApplicationEnvironment environment,
|
IApplicationEnvironment environment,
|
||||||
IAssemblyLoadContextAccessor loaderAccessor,
|
|
||||||
ILibraryExporter libraryExporter,
|
ILibraryExporter libraryExporter,
|
||||||
ICompilerOptionsProvider compilerOptionsProvider,
|
ICompilerOptionsProvider compilerOptionsProvider,
|
||||||
IMvcRazorHost host,
|
IMvcRazorHost host,
|
||||||
IOptions<RazorViewEngineOptions> optionsAccessor)
|
IOptions<RazorViewEngineOptions> optionsAccessor)
|
||||||
{
|
{
|
||||||
_environment = environment;
|
_environment = environment;
|
||||||
_loader = loaderAccessor.GetLoadContext(typeof(RoslynCompilationService).GetTypeInfo().Assembly);
|
|
||||||
_libraryExporter = libraryExporter;
|
_libraryExporter = libraryExporter;
|
||||||
_applicationReferences = new Lazy<List<MetadataReference>>(GetApplicationReferences);
|
_applicationReferences = new Lazy<List<MetadataReference>>(GetApplicationReferences);
|
||||||
_compilerOptionsProvider = compilerOptionsProvider;
|
_compilerOptionsProvider = compilerOptionsProvider;
|
||||||
_fileProvider = optionsAccessor.Value.FileProvider;
|
_fileProvider = optionsAccessor.Value.FileProvider;
|
||||||
_classPrefix = host.MainClassNamePrefix;
|
_classPrefix = host.MainClassNamePrefix;
|
||||||
|
|
||||||
|
#if DOTNET5_4
|
||||||
|
_razorLoadContext = new RazorLoadContext();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|
@ -131,11 +139,11 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
if (_supportsPdbGeneration.Value)
|
if (_supportsPdbGeneration.Value)
|
||||||
{
|
{
|
||||||
pdb.Seek(0, SeekOrigin.Begin);
|
pdb.Seek(0, SeekOrigin.Begin);
|
||||||
assembly = _loader.LoadStream(ms, pdb);
|
assembly = LoadStream(ms, pdb);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
assembly = _loader.LoadStream(ms, assemblySymbols: null);
|
assembly = LoadStream(ms, assemblySymbols: null);
|
||||||
}
|
}
|
||||||
|
|
||||||
var type = assembly.GetExportedTypes()
|
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)
|
private CSharpCompilation Rewrite(CSharpCompilation compilation)
|
||||||
{
|
{
|
||||||
var rewrittenTrees = new List<SyntaxTree>();
|
var rewrittenTrees = new List<SyntaxTree>();
|
||||||
|
|
@ -321,5 +338,20 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
|
|
||||||
return null;
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@
|
||||||
"dotnet5.4": {
|
"dotnet5.4": {
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"System.Text.Encoding": "4.0.11-*",
|
"System.Text.Encoding": "4.0.11-*",
|
||||||
|
"System.Runtime.Loader": "4.0.0-*",
|
||||||
"System.Threading.Tasks.Parallel": "4.0.1-beta-*"
|
"System.Threading.Tasks.Parallel": "4.0.1-beta-*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue