public VirtualPathRazorPageFactory(string rootDirectory, IExecutionContext executionContext, Type basePageType) { _rootDirectory = rootDirectory; _fileProvider = new PhysicalFileProvider(rootDirectory); _razorcompilationService = new RazorCompilationService(executionContext, basePageType); _executionContext = executionContext; }
private CompilationResult GetCompilation(RelativeFileInfo relativeFileInfo, IRazorCompilationService razorCompilationService) { CompilationResult compilationResult = razorCompilationService.Compile(relativeFileInfo); compilationResult.EnsureSuccessful(); return(compilationResult); }
/// <summary> /// Initializes a new instance of <see cref="DefaultRazorPageFactoryProvider"/>. /// </summary> /// <param name="razorCompilationService">The <see cref="IRazorCompilationService"/>.</param> /// <param name="compilerCacheProvider">The <see cref="ICompilerCacheProvider"/>.</param> public DefaultRazorPageFactoryProvider( IRazorCompilationService razorCompilationService, ICompilerCacheProvider compilerCacheProvider) { _compileDelegate = razorCompilationService.Compile; _compilerCacheProvider = compilerCacheProvider; }
/// <summary> /// Initializes a new instance of <see cref="VirtualPathRazorPageFactory"/>. /// </summary> /// <param name="razorCompilationService">The <see cref="IRazorCompilationService"/>.</param> /// <param name="compilerCacheProvider">The <see cref="ICompilerCacheProvider"/>.</param> public VirtualPathRazorPageFactory( IRazorCompilationService razorCompilationService, ICompilerCacheProvider compilerCacheProvider) { _compileDelegate = razorCompilationService.Compile; _compilerCacheProvider = compilerCacheProvider; }
public RazorCompilationServiceSpy(ICompilationService compilationService, IMvcRazorHost razorHost, IOptions <RazorViewEngineOptions> options) { _inner = new RazorCompilationService(compilationService, razorHost, options); _log = new List <CompileEntry>(); }
/// <summary> /// Gets the Razor page for an input document stream. This is roughly modeled on /// DefaultRazorPageFactory and CompilerCache. Note that we don't actually bother /// with caching the page if it's from a live stream. /// </summary> private IRazorPage GetPageFromStream( string relativePath, string viewStartLocation, string layoutLocation, Stream stream, IFileProvider rootFileProvider, IRazorCompilationService razorCompilationService) { if (relativePath.StartsWith("~/", StringComparison.Ordinal)) { // For tilde slash paths, drop the leading ~ to make it work with the underlying IFileProvider. relativePath = relativePath.Substring(1); } // Get the file info by combining the stream content with info found at the document's original location (if any) IFileInfo fileInfo = new StreamFileInfo(rootFileProvider.GetFileInfo(relativePath), stream); RelativeFileInfo relativeFileInfo = new RelativeFileInfo(fileInfo, relativePath); // Try to get the compilation from the cache, but only if the stream is empty // Cache key is relative path if no explicit view start or layout OR either/both of those if specified CompilationResult compilationResult = stream.Length == 0 ? _compilationCache.GetOrAdd( viewStartLocation == null ? (layoutLocation ?? relativePath) : (layoutLocation == null ? viewStartLocation : viewStartLocation + layoutLocation), _ => GetCompilation(relativeFileInfo, razorCompilationService)) : GetCompilation(relativeFileInfo, razorCompilationService); // Create and return the page // We're not actually using the ASP.NET cache, but the CompilerCacheResult ctor contains the logic to create the page factory CompilerCacheResult compilerCacheResult = new CompilerCacheResult(relativePath, compilationResult, Array.Empty <IChangeToken>()); return(compilerCacheResult.PageFactory()); }
/// <summary> /// Gets the view for an input document (which is different than the view for a layout, partial, or /// other indirect view because it's not necessarily on disk or in the file system). /// </summary> private IView GetViewFromStream( string relativePath, Stream stream, string viewStartLocation, string layoutLocation, IRazorViewEngine viewEngine, IRazorPageActivator pageActivator, HtmlEncoder htmlEncoder, IRazorPageFactoryProvider pageFactoryProvider, IFileProvider rootFileProvider, IRazorCompilationService razorCompilationService) { IEnumerable <string> viewStartLocations = viewStartLocation != null ? new [] { viewStartLocation } : ViewHierarchyUtility.GetViewStartLocations(relativePath); List <IRazorPage> viewStartPages = viewStartLocations .Select(pageFactoryProvider.CreateFactory) .Where(x => x.Success) .Select(x => x.RazorPageFactory()) .Reverse() .ToList(); IRazorPage page = GetPageFromStream(relativePath, viewStartLocation, layoutLocation, stream, rootFileProvider, razorCompilationService); if (layoutLocation != null) { page.Layout = layoutLocation; } return(new RazorView(viewEngine, pageActivator, viewStartPages, page, htmlEncoder)); }
public RazorCompilationServiceSpy(ICompilationService compilationService, IMvcRazorHost razorHost, IRazorViewEngineFileProviderAccessor fileProviderAccessor, ILoggerFactory logger) { _inner = new RazorCompilationService(compilationService, razorHost, fileProviderAccessor, logger); _log = new List <CompileEntry>(); }
public VirtualPathRazorPageFactory(IRazorCompilationService compilationService, ITypeActivator typeActivator, IServiceProvider serviceProvider, IFileInfoCache fileInfoCache) { _compilationService = compilationService; _activator = typeActivator; _serviceProvider = serviceProvider; _fileInfoCache = fileInfoCache; }
private CompilerCacheResult CompilePage(IServiceProvider serviceProvider, RenderRequest request, byte[] hash, RelativeFileInfo relativeFileInfo, string relativePath) { CompilerCacheKey cacheKey = new CompilerCacheKey(request, hash); IRazorCompilationService razorCompilationService = serviceProvider.GetRequiredService <IRazorCompilationService>(); CompilationResult compilationResult = _compilationCache.GetOrAdd(cacheKey, _ => GetCompilation(relativeFileInfo, razorCompilationService)); // Create and return the page // We're not actually using the ASP.NET cache, but the CompilerCacheResult ctor contains the logic to create the page factory CompilerCacheResult compilerCacheResult = new CompilerCacheResult(relativePath, compilationResult, Array.Empty <IChangeToken>()); return(compilerCacheResult); }
public ModulesRazorPageFactoryProvider( IRazorCompilationService razorCompilationService, ICompilerCacheProvider compilerCacheProvider, IHostingEnvironment moduleEnv, IRootServiceProvider rootServices) { _appProvider = rootServices.GetService <IRazorPageFactoryProvider>(); if (_appProvider == null) { throw new ArgumentNullException(nameof(_appProvider)); } _defaultProvider = new DefaultRazorPageFactoryProvider(razorCompilationService, compilerCacheProvider); _moduleEnv = moduleEnv; _rootEnv = rootServices.GetRequiredService <IHostingEnvironment>(); }
/// <summary> /// Gets the Razor page for an input document stream. This is roughly modeled on /// DefaultRazorPageFactory and CompilerCache. Note that we don't actually bother /// with caching the page if it's from a live stream. /// </summary> private IRazorPage GetPageFromStream(string relativePath, Stream stream, IFileProvider rootFileProvider, IRazorCompilationService razorCompilationService) { if (relativePath.StartsWith("~/", StringComparison.Ordinal)) { // For tilde slash paths, drop the leading ~ to make it work with the underlying IFileProvider. relativePath = relativePath.Substring(1); } // Get the file info by combining the stream content with info found at the document's original location (if any) IFileInfo fileInfo = new StreamFileInfo(rootFileProvider.GetFileInfo(relativePath), stream); RelativeFileInfo relativeFileInfo = new RelativeFileInfo(fileInfo, relativePath); // Create the compilation CompilationResult compilationResult = razorCompilationService.Compile(relativeFileInfo); compilationResult.EnsureSuccessful(); // Create and return the page // We're not actually using the cache, but the CompilerCacheResult ctor contains the logic to create the page factory CompilerCacheResult compilerCacheResult = new CompilerCacheResult(relativePath, compilationResult, Array.Empty <IChangeToken>()); return(compilerCacheResult.PageFactory()); }
public VirtualPathRazorPageFactory(IExecutionContext executionContext, Type basePageType) { _fileProvider = new WyamFileProvider(executionContext.FileSystem); _razorcompilationService = new RazorCompilationService(executionContext, basePageType); _executionContext = executionContext; }
public IEnumerable <IDocument> Execute(IReadOnlyList <IDocument> inputs, IExecutionContext context) { // Register all the MVC and Razor services // In the future, if DI is implemented for all Wyam, the IExecutionContext would be registered as a service // and the IHostingEnviornment would be registered as transient with the execution context provided in ctor IServiceCollection serviceCollection = new ServiceCollection(); IMvcCoreBuilder builder = serviceCollection .AddMvcCore() .AddRazorViewEngine(); builder.PartManager.FeatureProviders.Add(new MetadataReferenceFeatureProvider(context)); serviceCollection.Configure <RazorViewEngineOptions>(options => { options.ViewLocationExpanders.Add(new ViewLocationExpander()); }); serviceCollection .AddSingleton <ILoggerFactory, TraceLoggerFactory>() .AddSingleton <DiagnosticSource, SilentDiagnosticSource>() .AddSingleton <IHostingEnvironment, HostingEnvironment>() .AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>() .AddSingleton <IExecutionContext>(context) .AddSingleton <IBasePageTypeProvider>(new BasePageTypeProvider(_basePageType ?? typeof(RazorPage))) .AddScoped <IMvcRazorHost, RazorHost>(); IServiceProvider services = serviceCollection.BuildServiceProvider(); // Eliminate input documents that we shouldn't process List <IDocument> validInputs = inputs .Where(x => _ignorePrefix == null || !x.ContainsKey(Keys.SourceFileName) || !x.FilePath(Keys.SourceFileName).FullPath.StartsWith(_ignorePrefix)) .ToList(); // Compile and evaluate the pages in parallel IServiceScopeFactory scopeFactory = services.GetRequiredService <IServiceScopeFactory>(); return(validInputs.AsParallel().Select(input => { Trace.Verbose("Compiling Razor for {0}", input.SourceString()); using (var scope = scopeFactory.CreateScope()) { // Get services IRazorViewEngine viewEngine = services.GetRequiredService <IRazorViewEngine>(); IRazorPageActivator pageActivator = services.GetRequiredService <IRazorPageActivator>(); HtmlEncoder htmlEncoder = services.GetRequiredService <HtmlEncoder>(); IRazorPageFactoryProvider pageFactoryProvider = services.GetRequiredService <IRazorPageFactoryProvider>(); IRazorCompilationService razorCompilationService = services.GetRequiredService <IRazorCompilationService>(); IHostingEnvironment hostingEnviornment = services.GetRequiredService <IHostingEnvironment>(); // Compile the view string relativePath = GetRelativePath(input, context); FilePath viewStartLocationPath = _viewStartPath?.Invoke <FilePath>(input, context); string viewStartLocation = viewStartLocationPath != null ? GetRelativePath(viewStartLocationPath, context) : null; string layoutLocation = _layoutPath?.Invoke <FilePath>(input, context)?.FullPath; IView view; using (Stream stream = input.GetStream()) { view = GetViewFromStream(relativePath, stream, viewStartLocation, layoutLocation, viewEngine, pageActivator, htmlEncoder, pageFactoryProvider, hostingEnviornment.WebRootFileProvider, razorCompilationService); } // Render the view Trace.Verbose("Processing Razor for {0}", input.SourceString()); using (StringWriter output = new StringWriter()) { Microsoft.AspNetCore.Mvc.Rendering.ViewContext viewContext = GetViewContext(scope.ServiceProvider, view, input, context, output); viewContext.View.RenderAsync(viewContext).GetAwaiter().GetResult(); return context.GetDocument(input, output.ToString()); } } })); }