Exemplo n.º 1
0
        internal RazorCompiler(CompilationParameters parameters)
        {
            ServiceCollection serviceCollection = new ServiceCollection();

            IMvcCoreBuilder builder = serviceCollection
                                      .AddMvcCore()
                                      .AddRazorViewEngine();

            builder.PartManager.FeatureProviders.Add(new MetadataReferenceFeatureProvider(parameters.DynamicAssemblies));
            serviceCollection.Configure <RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new ViewLocationExpander());
            });

            // Disables the configuration of the DataProtection services, which we don't need for just razor generation.
            serviceCollection.AddTransient <IXmlRepository, DummyXmlRepository>();

            serviceCollection
            .AddSingleton(parameters.FileSystem)
            .AddSingleton <ILoggerFactory, TraceLoggerFactory>()
            .AddSingleton <ILoggerFactory, TraceLoggerFactory>()
            .AddSingleton <DiagnosticSource, SilentDiagnosticSource>()
            .AddSingleton <IHostingEnvironment, HostingEnvironment>()
            .AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>()
            .AddSingleton(parameters.Namespaces)
            .AddSingleton(parameters.DynamicAssemblies)
            .AddSingleton <IBasePageTypeProvider>(new BasePageTypeProvider(parameters.BasePageType ?? typeof(WyamRazorPage <>)))
            .AddScoped <IMvcRazorHost, RazorHost>();

            IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            _serviceScopeFactory = serviceProvider.GetRequiredService <IServiceScopeFactory>();
        }
        public void Render(RenderRequest request)
        {
            CompilationParameters parameters = new CompilationParameters
            {
                BasePageType      = request.BaseType,
                DynamicAssemblies = new DynamicAssemblyCollection(request.Context.DynamicAssemblies),
                Namespaces        = new NamespaceCollection(request.Context.Namespaces),
                FileSystem        = request.Context.FileSystem
            };

            RazorCompiler compiler = _compilers.GetOrAdd(parameters, _ => new RazorCompiler(parameters));

            compiler.RenderPage(request);
        }
        public RazorCompiler(CompilationParameters parameters)
        {
            _namespaces = parameters.Namespaces;

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

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

            // Create the service collection that MVC needs and add default MVC services
            ServiceCollection serviceCollection = new ServiceCollection();

            // Register some of our own types
            serviceCollection
            .AddSingleton(parameters.FileSystem)
            .AddSingleton <FileSystemFileProvider>()
            .AddSingleton <ILoggerFactory, TraceLoggerFactory>()
            .AddSingleton <DiagnosticSource, SilentDiagnosticSource>()
            .AddSingleton <IHostingEnvironment, HostingEnvironment>()
            .AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>()
            .AddSingleton <IRazorViewEngineFileProviderAccessor, DefaultRazorViewEngineFileProviderAccessor>()
            .AddSingleton <IViewCompilerProvider, WyamRazorViewCompilerProvider>()
            .AddSingleton <WyamRazorProjectFileSystem>()
            .AddSingleton <RazorProjectFileSystem, WyamRazorProjectFileSystem>()
            .AddSingleton <RazorProjectEngine>(x =>
                                               RazorProjectEngine.Create(
                                                   RazorConfiguration.Default,
                                                   x.GetRequiredService <RazorProjectFileSystem>(),
                                                   b =>
            {
                // See MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngineServices(IServiceCollection)
                RazorExtensions.Register(b);
                b.Features.Add(x.GetRequiredService <LazyMetadataReferenceFeature>());            // Lazily calls the MetadataReferenceFeatureProvider
                b.Features.Add(new CompilationTagHelperFeature());
                b.Features.Add(new DefaultTagHelperDescriptorProvider());
                b.Features.Add(new ViewComponentTagHelperDescriptorProvider());

                // 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
                b.Phases.Insert(
                    b.Phases.IndexOf(b.Phases.OfType <IRazorDocumentClassifierPhase>().Last()) + 1,
                    new WyamDocumentPhase(_baseType, _namespaces));
            }));

            // Register the view location expander
            serviceCollection.Configure <RazorViewEngineOptions>(options =>
            {
                options.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();

            // Get and register MetadataReferences
            builder.PartManager.FeatureProviders.Add(
                new MetadataReferenceFeatureProvider(parameters.DynamicAssemblies));

            IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            _serviceScopeFactory = serviceProvider.GetRequiredService <IServiceScopeFactory>();
        }