Exemple #1
0
        public async Task RuntimeErrorAreListedByErrorPageMiddleware()
        {
            // The desktop CLR does not correctly read the stack trace from portable PDBs. However generating full pdbs
            // is only supported on machines with CLSID_CorSymWriter available. On desktop, we'll skip this test on
            // machines without this component.
#if NET451
            if (!SymbolsUtility.SupportsFullPdbGeneration())
            {
                return;
            }
#endif

            // Arrange
            var expectedMessage   = HtmlEncoder.Default.Encode("throw new Exception(\"Error from view\");");
            var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");

            // Act
            var response = await Client.GetAsync("http://localhost/RuntimeError");

            // Assert
            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
            var content = await response.Content.ReadAsStringAsync();

            Assert.Contains("/Views/ErrorPageMiddleware/RuntimeError.cshtml", content);
            Assert.Contains(expectedMessage, content);
        }
        public RoslynCompilationService(IMetadataReferenceManager referenceManager, Assembly operatingAssembly)
        {
            if (referenceManager == null)
            {
                throw new ArgumentNullException(nameof(referenceManager));
            }

            if (operatingAssembly == null)
            {
                throw new ArgumentNullException(nameof(operatingAssembly));
            }

            metadataReferenceManager = referenceManager;
            this.operatingAssembly   = operatingAssembly;

            isDevelopment = IsAssemblyDebugBuild(OperatingAssembly);

            var pdbFormat = SymbolsUtility.SupportsFullPdbGeneration() ?
                            DebugInformationFormat.Pdb :
                            DebugInformationFormat.PortablePdb;

            EmitOptions = new EmitOptions(debugInformationFormat: pdbFormat);


            var cacheOptions = Options.Create(new MemoryCacheOptions());

            _cache = new MemoryCache(cacheOptions);
        }
Exemple #3
0
        protected virtual RazorFileInfoCollection GeneratePrecompiledAssembly(
            [NotNull] IEnumerable <SyntaxTree> syntaxTrees,
            [NotNull] IEnumerable <RazorFileInfo> razorFileInfos)
        {
            var resourcePrefix = string.Join(".", CompileContext.Compilation.AssemblyName,
                                             nameof(RazorPreCompiler),
                                             Path.GetRandomFileName());
            var assemblyResourceName = resourcePrefix + ".dll";


            var applicationReference = CompileContext.Compilation.ToMetadataReference();
            var references           = CompileContext.Compilation.References
                                       .Concat(new[] { applicationReference });

            var preCompilationOptions = CompilationSettings.CompilationOptions
                                        .WithOutputKind(OutputKind.DynamicallyLinkedLibrary);

            var compilation = CSharpCompilation.Create(assemblyResourceName,
                                                       options: preCompilationOptions,
                                                       syntaxTrees: syntaxTrees,
                                                       references: references);

            var generateSymbols = GenerateSymbols && SymbolsUtility.SupportsSymbolsGeneration();
            // These streams are returned to the runtime and consequently cannot be disposed.
            var assemblyStream = new MemoryStream();
            var pdbStream      = generateSymbols ? new MemoryStream() : null;
            var emitResult     = compilation.Emit(assemblyStream, pdbStream);

            if (!emitResult.Success)
            {
                AddRange(CompileContext.Diagnostics, emitResult.Diagnostics);
                return(null);
            }
            else
            {
                assemblyStream.Position = 0;
                var assemblyResource = new ResourceDescription(assemblyResourceName,
                                                               () => assemblyStream,
                                                               isPublic: true);
                CompileContext.Resources.Add(assemblyResource);

                string symbolsResourceName = null;
                if (pdbStream != null)
                {
                    symbolsResourceName = resourcePrefix + ".pdb";
                    pdbStream.Position  = 0;

                    var pdbResource = new ResourceDescription(symbolsResourceName,
                                                              () => pdbStream,
                                                              isPublic: true);

                    CompileContext.Resources.Add(pdbResource);
                }

                return(new PrecompileRazorFileInfoCollection(assemblyResourceName,
                                                             symbolsResourceName,
                                                             razorFileInfos.ToList()));
            }
        }
Exemple #4
0
        public RoslynCompilationService(IMetadataReferenceManager referenceManager, Assembly operatingAssembly)
        {
            this.metadataReferenceManager = referenceManager ?? throw new ArgumentNullException(nameof(referenceManager));
            this.OperatingAssembly        = operatingAssembly ?? throw new ArgumentNullException(nameof(operatingAssembly));

            isDevelopment = AssemblyDebugModeUtility.IsAssemblyDebugBuild(OperatingAssembly);
            var pdbFormat = SymbolsUtility.SupportsFullPdbGeneration() ?
                            DebugInformationFormat.Pdb :
                            DebugInformationFormat.PortablePdb;

            EmitOptions = new EmitOptions(debugInformationFormat: pdbFormat);
        }