parent
cda1307848
commit
b4310ac8a8
|
|
@ -32,7 +32,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DefaultRoslynCompilationService : ICompilationService
|
public class DefaultRoslynCompilationService : ICompilationService
|
||||||
{
|
{
|
||||||
private readonly Lazy<bool> _supportsPdbGeneration = new Lazy<bool>(SymbolsUtility.SupportsSymbolsGeneration);
|
|
||||||
private readonly ConcurrentDictionary<string, AssemblyMetadata> _metadataFileCache =
|
private readonly ConcurrentDictionary<string, AssemblyMetadata> _metadataFileCache =
|
||||||
new ConcurrentDictionary<string, AssemblyMetadata>(StringComparer.OrdinalIgnoreCase);
|
new ConcurrentDictionary<string, AssemblyMetadata>(StringComparer.OrdinalIgnoreCase);
|
||||||
private readonly IFileProvider _fileProvider;
|
private readonly IFileProvider _fileProvider;
|
||||||
|
|
@ -112,13 +111,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
|
||||||
path: assemblyName,
|
path: assemblyName,
|
||||||
options: _parseOptions);
|
options: _parseOptions);
|
||||||
|
|
||||||
var references = _applicationReferences.Value;
|
|
||||||
|
|
||||||
var compilation = CSharpCompilation.Create(
|
var compilation = CSharpCompilation.Create(
|
||||||
assemblyName,
|
assemblyName,
|
||||||
options: _compilationOptions,
|
options: _compilationOptions,
|
||||||
syntaxTrees: new[] { syntaxTree },
|
syntaxTrees: new[] { syntaxTree },
|
||||||
references: references);
|
references: _applicationReferences.Value);
|
||||||
|
|
||||||
compilation = Rewrite(compilation);
|
compilation = Rewrite(compilation);
|
||||||
|
|
||||||
|
|
@ -126,20 +123,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
|
||||||
_compilationCallback(compilationContext);
|
_compilationCallback(compilationContext);
|
||||||
compilation = compilationContext.Compilation;
|
compilation = compilationContext.Compilation;
|
||||||
|
|
||||||
using (var ms = new MemoryStream())
|
using (var assemblyStream = new MemoryStream())
|
||||||
{
|
{
|
||||||
using (var pdb = new MemoryStream())
|
using (var pdbStream = new MemoryStream())
|
||||||
{
|
{
|
||||||
EmitResult result;
|
var result = compilation.Emit(
|
||||||
|
assemblyStream,
|
||||||
if (_supportsPdbGeneration.Value)
|
pdbStream,
|
||||||
{
|
options: new EmitOptions(debugInformationFormat: DebugInformationFormat.PortablePdb));
|
||||||
result = compilation.Emit(ms, pdbStream: pdb);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = compilation.Emit(ms);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!result.Success)
|
if (!result.Success)
|
||||||
{
|
{
|
||||||
|
|
@ -162,19 +153,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
|
||||||
result.Diagnostics);
|
result.Diagnostics);
|
||||||
}
|
}
|
||||||
|
|
||||||
Assembly assembly;
|
assemblyStream.Seek(0, SeekOrigin.Begin);
|
||||||
ms.Seek(0, SeekOrigin.Begin);
|
pdbStream.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
if (_supportsPdbGeneration.Value)
|
|
||||||
{
|
|
||||||
pdb.Seek(0, SeekOrigin.Begin);
|
|
||||||
assembly = LoadStream(ms, pdb);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
assembly = LoadStream(ms, assemblySymbols: null);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
var assembly = LoadStream(assemblyStream, pdbStream);
|
||||||
var type = assembly.GetExportedTypes().FirstOrDefault(a => !a.IsNested);
|
var type = assembly.GetExportedTypes().FirstOrDefault(a => !a.IsNested);
|
||||||
_logger.GeneratedCodeToAssemblyCompilationEnd(fileInfo.RelativePath, startTimestamp);
|
_logger.GeneratedCodeToAssemblyCompilationEnd(fileInfo.RelativePath, startTimestamp);
|
||||||
|
|
||||||
|
|
@ -186,7 +168,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
|
||||||
private Assembly LoadStream(MemoryStream ms, MemoryStream assemblySymbols)
|
private Assembly LoadStream(MemoryStream ms, MemoryStream assemblySymbols)
|
||||||
{
|
{
|
||||||
#if NET451
|
#if NET451
|
||||||
return Assembly.Load(ms.ToArray(), assemblySymbols?.ToArray());
|
return Assembly.Load(ms.ToArray(), assemblySymbols.ToArray());
|
||||||
#else
|
#else
|
||||||
return _razorLoadContext.Load(ms, assemblySymbols);
|
return _razorLoadContext.Load(ms, assemblySymbols);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,46 +0,0 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc.Razor.Internal
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Utility type for determining if a platform supports symbol file generation.
|
|
||||||
/// </summary>
|
|
||||||
public class SymbolsUtility
|
|
||||||
{
|
|
||||||
private const string SymWriterGuid = "0AE2DEB0-F901-478b-BB9F-881EE8066788";
|
|
||||||
private static readonly Lazy<bool> _isMono = new Lazy<bool>(() => Type.GetType("Mono.Runtime") != null);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if the current platform supports symbols (pdb) generation.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns><c>true</c> if pdb generation is supported; <c>false</c> otherwise.</returns>
|
|
||||||
public static bool SupportsSymbolsGeneration()
|
|
||||||
{
|
|
||||||
if (_isMono.Value)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Check for the pdb writer component that roslyn uses to generate pdbs
|
|
||||||
var type = Marshal.GetTypeFromCLSID(new Guid(SymWriterGuid));
|
|
||||||
if (type != null)
|
|
||||||
{
|
|
||||||
// This line will throw if pdb generation is not supported.
|
|
||||||
Activator.CreateInstance(type);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue