private CompilationResult GetCompilation(RelativeFileInfo relativeFileInfo, IRazorCompilationService razorCompilationService) { CompilationResult compilationResult = razorCompilationService.Compile(relativeFileInfo); compilationResult.EnsureSuccessful(); return(compilationResult); }
/// <inheritdoc /> public IRazorPage CreateInstance([NotNull] string path) { var fileInfo = _fileInfoCache.GetFileInfo(path); if (fileInfo != null) { var result = _compilationService.Compile(fileInfo); var page = (IRazorPage)_activator.CreateInstance(_serviceProvider, result.CompiledType); page.Path = path; return(page); } return(null); }
public IRazorPage CreateInstance([NotNull] string relativePath, Stream stream) { if (relativePath.StartsWith("~/", StringComparison.Ordinal)) { // For tilde slash paths, drop the leading ~ to make it work with the underlying IFileProvider. relativePath = relativePath.Substring(1); } // Code below is taken from CompilerCache (specifically OnCacheMiss) which is responsible for managing the compilation step in MVC // Check the file var fileProvider = stream == null ? (IFileProvider)_fileProvider : new WyamStreamFileProvider(_fileProvider, stream); var fileInfo = fileProvider.GetFileInfo(relativePath); if (!fileInfo.Exists) { return(null); } // If relative path is the root, it probably means this isn't from reading a file so don't bother with caching IExecutionCache cache = relativePath == "/" ? null : _executionContext.ExecutionCache; string key = null; Type pageType = null; if (cache != null) { key = relativePath + " " + RazorFileHash.GetHash(fileInfo); if (!cache.TryGetValue(key, out pageType)) { pageType = null; } } // Compile and store in cache if not found if (pageType == null) { var relativeFileInfo = new RelativeFileInfo(fileInfo, relativePath); pageType = _razorcompilationService.Compile(relativeFileInfo); cache?.Set(key, pageType); } // Create an return a new page instance IRazorPage page = (IRazorPage)Activator.CreateInstance(pageType); page.Path = relativePath; return(page); }
/// <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()); }