diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs index e9dfea547b..9d2a2962ee 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.InteropServices; using Microsoft.AspNet.FileSystems; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -20,6 +21,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation /// public class RoslynCompilationService : ICompilationService { + private readonly Lazy _supportsPdbGeneration = new Lazy(SupportsPdbGeneration); private readonly ConcurrentDictionary _metadataFileCache = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); @@ -70,13 +72,13 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation { EmitResult result; - if (PlatformHelper.IsMono) + if (_supportsPdbGeneration.Value) { - result = compilation.Emit(ms, pdbStream: null); + result = compilation.Emit(ms, pdbStream: pdb); } else { - result = compilation.Emit(ms, pdbStream: pdb); + result = compilation.Emit(ms); } if (!result.Success) @@ -94,15 +96,15 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation Assembly assembly; ms.Seek(0, SeekOrigin.Begin); - if (PlatformHelper.IsMono) - { - assembly = _loader.LoadStream(ms, pdbStream: null); - } - else + if (_supportsPdbGeneration.Value) { pdb.Seek(0, SeekOrigin.Begin); assembly = _loader.LoadStream(ms, pdb); } + else + { + assembly = _loader.LoadStream(ms, pdbStream: null); + } var type = assembly.GetExportedTypes() .First(t => t.Name. @@ -187,5 +189,25 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation { return diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error; } + + private static bool SupportsPdbGeneration() + { + try + { + if (PlatformHelper.IsMono) + { + return false; + } + + // Check for the pdb writer component that roslyn uses to generate pdbs + const string SymWriterGuid = "0AE2DEB0-F901-478b-BB9F-881EE8066788"; + + return Marshal.GetTypeFromCLSID(new Guid(SymWriterGuid)) != null; + } + catch + { + return false; + } + } } }