public string GenerateMessageBody(string template, object model) { string result = null; try { var engine = EngineFactory.CreateEmbedded(model.GetType()); //result = engine.ParseString(template, model); ITemplateSource source = new LoadedTemplateSource(template); ModelTypeInfo modelInfo = new ModelTypeInfo(model.GetType()); CompilationResult compiled = engine.Core.CompileSource(source, modelInfo); compiled.EnsureSuccessful(); TemplatePage page = engine.Activate(compiled.CompiledType); page.PageContext = new PageContext { ModelTypeInfo = modelInfo }; result = engine.RunTemplate(page, model); } catch (TemplateCompilationException tex) { throw new MessageGenerationException($"Error generating message from template! {tex.CompilationErrors.FirstOrDefault()}", tex); } catch (Exception ex) { throw new MessageGenerationException("Unexpected error generating message from template!", ex); } return(result); }
private Task <CompilerCacheResult> CreateCacheEntry( string key, Func <string, CompilationResult> compile) { MemoryCacheEntryOptions cacheEntryOptions = GetMemoryCacheEntryOptions(); CompilationResult compilationResult = compile(key); compilationResult.EnsureSuccessful(); var result = new CompilerCacheResult(key, compilationResult, cacheEntryOptions.ExpirationTokens); return(Task.FromResult(result)); }
public PageFactoryResult CreateFactory(string key) { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException(nameof(key)); } CompilationResult result = _compileDelegate(key); result.EnsureSuccessful(); var pageFactory = new Func <TemplatePage>(() => (TemplatePage)Activator.CreateInstance(result.CompiledType)); return(new PageFactoryResult(pageFactory, expirationTokens: null)); }
/// <summary> /// Parses a string /// </summary> /// <param name="content">Template to parse</param> /// <param name="model">Template model</param> /// <param name="modelType">Type of the model</param> /// <returns></returns> public static string ParseString(this IRazorLightEngine engine, string content, object model, Type modelType) { if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException(nameof(content)); } ITemplateSource templateSource = new LoadedTemplateSource(content); ModelTypeInfo modelTypeInfo = new ModelTypeInfo(modelType); CompilationResult result = engine.Core.CompileSource(templateSource, modelTypeInfo); result.EnsureSuccessful(); TemplatePage page = engine.Activate(result.CompiledType); page.PageContext = new PageContext() { ModelTypeInfo = modelTypeInfo }; return(engine.RunTemplate(page, model)); }
private Task <CompilerCacheResult> CreateCacheEntry( string relativePath, string normalizedPath, Func <string, CompilationResult> compile) { TaskCompletionSource <CompilerCacheResult> compilationTaskSource = null; MemoryCacheEntryOptions cacheEntryOptions = null; IFileInfo fileInfo = null; Task <CompilerCacheResult> cacheEntry; // Safe races cannot be allowed when compiling Razor pages. To ensure only one compilation request succeeds // per file, we'll lock the creation of a cache entry. Creating the cache entry should be very quick. The // actual work for compiling files happens outside the critical section. lock (_cacheLock) { if (_cache.TryGetValue(normalizedPath, out cacheEntry)) { return(cacheEntry); } fileInfo = _fileProvider.GetFileInfo(normalizedPath); if (!fileInfo.Exists) { IChangeToken expirationToken = _fileProvider.Watch(normalizedPath); cacheEntry = Task.FromResult(new CompilerCacheResult(new[] { expirationToken })); cacheEntryOptions = new MemoryCacheEntryOptions(); cacheEntryOptions.AddExpirationToken(expirationToken); } else { cacheEntryOptions = GetMemoryCacheEntryOptions(normalizedPath); // A file exists and needs to be compiled. compilationTaskSource = new TaskCompletionSource <CompilerCacheResult>(); cacheEntry = compilationTaskSource.Task; } cacheEntry = _cache.Set <Task <CompilerCacheResult> >(normalizedPath, cacheEntry, cacheEntryOptions); } if (compilationTaskSource != null) { // Indicates that the file was found and needs to be compiled. Debug.Assert(fileInfo != null && fileInfo.Exists); Debug.Assert(cacheEntryOptions != null); try { CompilationResult compilationResult = compile(relativePath); compilationResult.EnsureSuccessful(); compilationTaskSource.SetResult( new CompilerCacheResult(relativePath, compilationResult, cacheEntryOptions.ExpirationTokens)); } catch (Exception ex) { compilationTaskSource.SetException(ex); } } return(cacheEntry); }