Exemplo n.º 1
0
        private WebPageBase CreatePage(ViewContext viewContext, System.IO.TextWriter writer, out WebPageContext pageContext, out WebPageRenderingBase startPage)
        {
            var basePage = (WebPageBase)Activator.CreateInstance(_viewType);

            basePage.VirtualPath        = _virtualPath;
            basePage.VirtualPathFactory = _viewEngine.VirtualPathFactory;

            pageContext = new WebPageContext(viewContext.HttpContext, basePage, viewContext.ViewData?.Model);

            startPage = _runViewStart
                ? StartPage.GetStartPage(basePage, "_ViewStart", _viewEngine.FileExtensions)
                : null;

            var viewPage = basePage as WebViewPage;

            if (viewPage != null)
            {
                if (!string.IsNullOrEmpty(_masterPath))
                {
                    Hacks.SetOverriddenLayoutPath(viewPage, _masterPath);
                }

                viewPage.ViewContext = viewContext;
                viewPage.ViewData    = viewContext.ViewData;
                viewPage.InitHelpers();
            }

            return(basePage);
        }
Exemplo n.º 2
0
        private static Assembly CompileToAssembly(WebPageRazorHost host, SyntaxTree syntaxTree)
        {
            var compilation = CSharpCompilation.Create(
                "RoslynRazor", // Note: using a fixed assembly name, which doesn't matter as long as we don't expect cross references of generated assemblies
                new[] { syntaxTree },
                BuildManager.GetReferencedAssemblies().OfType <Assembly>().Select(ResolveReference),
                new CSharpCompilationOptions(
                    outputKind: OutputKind.DynamicallyLinkedLibrary,
                    assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default,
                    optimizationLevel: host.DefaultDebugCompilation ? OptimizationLevel.Debug : OptimizationLevel.Release));

            compilation = Hacks.MakeValueTuplesWorkWhenRunningOn47RuntimeAndTargetingNet45Plus(compilation);

            using (var dllStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    EmitResult emitResult = compilation.Emit(dllStream, pdbStream);

                    if (!emitResult.Success)
                    {
                        Diagnostic   diagnostic   = emitResult.Diagnostics.First(x => x.Severity == DiagnosticSeverity.Error);
                        string       message      = diagnostic.ToString();
                        LinePosition linePosition = diagnostic.Location.GetMappedLineSpan().StartLinePosition;

                        throw new HttpParseException(message, null, host.VirtualPath, null, linePosition.Line + 1);
                    }

                    return(Assembly.Load(dllStream.GetBuffer(), pdbStream.GetBuffer()));
                }
        }
        private static Assembly CompileToAssembly(WebPageRazorHost host, SyntaxTree syntaxTree, ICollection <ICompileModule> compilationModules)
        {
            var strArgs = new List <string>();

            strArgs.Add("/target:library");
            strArgs.Add(host.DefaultDebugCompilation ? "/o-" : "/o+");
            strArgs.Add(host.DefaultDebugCompilation ? "/debug+" : "/debug-");

            var cscArgs = CSharpCommandLineParser.Default.Parse(strArgs, null, null);

            var compilation = CSharpCompilation.Create(
                "RoslynRazor", // Note: using a fixed assembly name, which doesn't matter as long as we don't expect cross references of generated assemblies
                new[] { syntaxTree },
                BuildManager.GetReferencedAssemblies().OfType <Assembly>().Select(ResolveReference),
                cscArgs.CompilationOptions.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default));

            compilation = Hacks.MakeValueTuplesWorkWhenRunningOn47RuntimeAndTargetingNet45Plus(compilation);
            var diagnostics = new List <Diagnostic>();
            var context     = new CompileContext(compilationModules);

            context.Before(new BeforeCompileContext
            {
                Arguments   = cscArgs,
                Compilation = compilation,
                Diagnostics = diagnostics,
            });
            compilation = context.BeforeCompileContext.Compilation;

            using (var dllStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    EmitResult emitResult = compilation.Emit(dllStream, pdbStream);
                    diagnostics.AddRange(emitResult.Diagnostics);

                    if (!emitResult.Success)
                    {
                        Diagnostic   diagnostic   = diagnostics.First(x => x.Severity == DiagnosticSeverity.Error);
                        string       message      = diagnostic.ToString();
                        LinePosition linePosition = diagnostic.Location.GetMappedLineSpan().StartLinePosition;

                        throw new HttpParseException(message, null, host.VirtualPath, null, linePosition.Line + 1);
                    }

                    context.After(new AfterCompileContext
                    {
                        Arguments      = context.BeforeCompileContext.Arguments,
                        AssemblyStream = dllStream,
                        Compilation    = compilation,
                        Diagnostics    = diagnostics,
                        SymbolStream   = pdbStream,
                        XmlDocStream   = null,
                    });

                    return(Assembly.Load(dllStream.GetBuffer(), pdbStream.GetBuffer()));
                }
        }