diff --git a/src/Common/PlatformHelper.cs b/src/Common/PlatformHelper.cs new file mode 100644 index 0000000000..dc1649d5ad --- /dev/null +++ b/src/Common/PlatformHelper.cs @@ -0,0 +1,17 @@ +using System; + +namespace Microsoft.AspNet.Mvc +{ + internal static class PlatformHelper + { + private static Lazy _isMono = new Lazy(() => Type.GetType("Mono.Runtime") != null); + + public static bool IsMono + { + get + { + return _isMono.Value; + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs index 51b86d9fed..e5f17a6b67 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; +using System.Collections.Concurrent; using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Emit; using Microsoft.CodeAnalysis.CSharp; using Microsoft.Net.Runtime; @@ -15,6 +17,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation private readonly ILibraryManager _libraryManager; private readonly IApplicationEnvironment _environment; private readonly IAssemblyLoaderEngine _loader; + private static readonly ConcurrentDictionary _metadataFileCache = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); public RoslynCompilationService(IApplicationEnvironment environment, IAssemblyLoaderEngine loaderEngine, @@ -43,7 +46,16 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation { using (var pdb = new MemoryStream()) { - var result = compilation.Emit(ms, pdbStream: pdb); + EmitResult result = null; + + if (PlatformHelper.IsMono) + { + result = compilation.Emit(ms, pdbStream: null); + } + else + { + result = compilation.Emit(ms, pdbStream: pdb); + } if (!result.Success) { @@ -54,8 +66,18 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation return CompilationResult.Failed(content, messages); } - var type = _loader.LoadBytes(ms.ToArray(), pdb.ToArray()) - .GetExportedTypes() + Assembly assembly = null; + + if (PlatformHelper.IsMono) + { + assembly = _loader.LoadBytes(ms.ToArray(), pdbBytes: null); + } + else + { + assembly = _loader.LoadBytes(ms.ToArray(), pdb.ToArray()); + } + + var type = assembly.GetExportedTypes() .First(); return CompilationResult.Successful(String.Empty, type); @@ -93,16 +115,15 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation private MetadataReference CreateMetadataFileReference(string path) { -#if NET45 - return new MetadataFileReference(path); -#else - // TODO: What about access to the file system? We need to be able to - // read files from anywhere on disk, not just under the web root - using (var stream = File.OpenRead(path)) + return _metadataFileCache.GetOrAdd(path, _ => { - return new MetadataImageReference(stream); - } -#endif + // TODO: What about access to the file system? We need to be able to + // read files from anywhere on disk, not just under the web root + using (var stream = File.OpenRead(path)) + { + return new MetadataImageReference(stream); + } + }); } private CompilationMessage GetCompilationMessage(DiagnosticFormatter formatter, Diagnostic diagnostic)