/// <summary>
        /// Adds all services required for the <see cref="RenderRazor"/> module.
        /// </summary>
        /// <param name="serviceCollection">The service collection to register services in.</param>
        /// <param name="fileSystem">The file system or <c>null</c> to skip.</param>
        /// <param name="classCatalog">An existing class catalog or <c>null</c> to scan assemblies during registration.</param>
        /// <returns>The service collection.</returns>
        public static IServiceCollection AddRazor(this IServiceCollection serviceCollection, IReadOnlyFileSystem fileSystem, ClassCatalog classCatalog = null)
        {
            // Register the file system if we're not expecting one from an engine
            if (fileSystem != null)
            {
                serviceCollection.TryAddSingleton(fileSystem);
            }

            // Register some of our own types if not already registered
            serviceCollection.TryAddSingleton <Microsoft.Extensions.FileProviders.IFileProvider, FileSystemFileProvider>();
            serviceCollection.TryAddSingleton <DiagnosticSource, SilentDiagnosticSource>();
            serviceCollection.TryAddSingleton(new DiagnosticListener("Razor"));
            serviceCollection.TryAddSingleton <IWebHostEnvironment, HostEnvironment>();
            serviceCollection.TryAddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>();
            serviceCollection.TryAddSingleton <StatiqRazorProjectFileSystem>();
            serviceCollection.TryAddSingleton <RazorProjectFileSystem, StatiqRazorProjectFileSystem>();

            // Register the view location expander if not already registered
            serviceCollection.Configure <RazorViewEngineOptions>(x =>
            {
                if (!x.ViewLocationExpanders.OfType <ViewLocationExpander>().Any())
                {
                    x.ViewLocationExpanders.Add(new ViewLocationExpander());
                }
            });

            // Add the default services _after_ adding our own
            // (most default registration use .TryAdd...() so they skip already registered types)
            IMvcCoreBuilder builder = serviceCollection
                                      .AddMvcCore()
                                      .AddRazorViewEngine()
                                      .AddRazorRuntimeCompilation();

            // Add all loaded assemblies
            CompilationReferencesProvider referencesProvider = new CompilationReferencesProvider();

            referencesProvider.Assemblies.AddRange((classCatalog ?? new ClassCatalog()).GetAssemblies());

            // And a couple needed assemblies that might not be loaded in the AppDomain yet
            referencesProvider.Assemblies.Add(typeof(IHtmlContent).Assembly);
            referencesProvider.Assemblies.Add(Assembly.Load(new AssemblyName("Microsoft.CSharp")));

            // Add the reference provider as an ApplicationPart
            builder.ConfigureApplicationPartManager(x => x.ApplicationParts.Add(referencesProvider));

            return(serviceCollection);
        }
        public RazorCompiler(CompilationParameters parameters, IExecutionContext context)
        {
            // Create the service collection that MVC needs and add default MVC services
            ServiceCollection serviceCollection = new ServiceCollection();

            DiagnosticListener listener = new DiagnosticListener("Razor");

            // Register some of our own types
            serviceCollection
            .AddSingleton(parameters.FileSystem)
            .AddSingleton <Microsoft.Extensions.FileProviders.IFileProvider, FileSystemFileProvider>()
            .AddSingleton(context.GetRequiredService <ILoggerFactory>())
            .AddSingleton <DiagnosticSource, SilentDiagnosticSource>()
            .AddSingleton <DiagnosticListener>(listener)
            .AddSingleton <IWebHostEnvironment, HostEnvironment>()
            .AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>()

            // .AddSingleton<IViewCompilerProvider, StatiqRazorViewCompilerProvider>()
            .AddSingleton <StatiqRazorProjectFileSystem>()
            .AddSingleton <RazorProjectFileSystem, StatiqRazorProjectFileSystem>();

            // Register the view location expander
            serviceCollection.Configure <RazorViewEngineOptions>(x => x.ViewLocationExpanders.Add(new ViewLocationExpander()));

            // Add the default services _after_ adding our own
            // (most default registration use .TryAdd...() so they skip already registered types)
            IMvcCoreBuilder builder = serviceCollection
                                      .AddMvcCore()
                                      .AddRazorViewEngine()
                                      .AddRazorRuntimeCompilation();

            // Add all loaded assemblies
            CompilationReferencesProvider referencesProvider = new CompilationReferencesProvider();

            referencesProvider.Assemblies.AddRange(AppDomain.CurrentDomain.GetAssemblies());

            // And a couple needed assemblies that might not be loaded in the AppDomain yet
            referencesProvider.Assemblies.Add(typeof(IHtmlContent).Assembly);
            referencesProvider.Assemblies.Add(Assembly.Load(new AssemblyName("Microsoft.CSharp")));

            // Add the reference provider as an ApplicationPart
            builder.ConfigureApplicationPartManager(x => x.ApplicationParts.Add(referencesProvider));

            // Build the service provider and local scope
            IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            _serviceScopeFactory = serviceProvider.GetRequiredService <IServiceScopeFactory>();

            // Calculate the base page type
            Type   basePageType  = parameters.BasePageType ?? typeof(StatiqRazorPage <>);
            string baseClassName = basePageType.FullName;
            int    tickIndex     = baseClassName.IndexOf('`');

            if (tickIndex > 0)
            {
                baseClassName = baseClassName.Substring(0, tickIndex);
            }
            string baseType = basePageType.IsGenericTypeDefinition ? $"{baseClassName}<TModel>" : baseClassName;

            // We need to register a new document classifier phase because builder.SetBaseType() (which uses builder.ConfigureClass())
            // use the DefaultRazorDocumentClassifierPhase which stops applying document classifier passes after DocumentIntermediateNode.DocumentKind is set
            // (which gets set by the Razor document classifier passes registered in RazorExtensions.Register())
            // Also need to add it just after the DocumentClassifierPhase, otherwise it'll miss the C# lowering phase
            RazorProjectEngine       razorProjectEngine = serviceProvider.GetRequiredService <RazorProjectEngine>();
            List <IRazorEnginePhase> phases             = razorProjectEngine.Engine.Phases.ToList();

            phases.Insert(
                phases.IndexOf(phases.OfType <IRazorDocumentClassifierPhase>().Last()) + 1,
                new StatiqDocumentPhase(baseType, parameters.Namespaces)
            {
                Engine = razorProjectEngine.Engine
            });
            FieldInfo phasesField = razorProjectEngine.Engine.GetType().GetField("<Phases>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);

            phasesField.SetValue(razorProjectEngine.Engine, phases.ToArray());
        }