Beispiel #1
0
        public RazorHost(NamespaceCollection namespaces, IChunkTreeCache chunkTreeCache, ITagHelperDescriptorResolver resolver, IBasePageTypeProvider basePageTypeProvider)
            : base(chunkTreeCache, resolver)
        {
            // Remove the backtick from generic class names
            string baseClassName = basePageTypeProvider.BasePageType.FullName;
            int    tickIndex     = baseClassName.IndexOf('`');

            if (tickIndex > 0)
            {
                baseClassName = baseClassName.Substring(0, tickIndex);
            }

            DefaultBaseClass = basePageTypeProvider.BasePageType.IsGenericTypeDefinition ? $"{baseClassName}<{ChunkHelper.TModelToken}>" : baseClassName;
            DefaultInheritedChunks.OfType <SetBaseTypeChunk>().First().TypeName = DefaultBaseClass;  // The chunk is actually what injects the base name into the view
            EnableInstrumentation = false;

            // Add additional default namespaces from the execution context
            foreach (string ns in namespaces)
            {
                NamespaceImports.Add(ns);
            }
        }
 public WyamDocumentPhase(string baseType, NamespaceCollection namespaces)
 {
     _baseType   = baseType;
     _namespaces = namespaces;
 }
        public override bool Equals(object obj)
        {
            NamespaceCollection other = obj as NamespaceCollection;

            return(other != null && _namespaces.SequenceEqual(other._namespaces));
        }
        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>();
        }