public void RazorEngineService_TestDebuggingWithHelperWithTemplateFile() { var cache = new DefaultCachingProvider(); var file = System.IO.Path.GetTempFileName(); var template = @"@helper Display(int price) { if (price == 0) { <text>free</text> } else { <text>@price</text> } } @Display(Model.MyPrice)"; File.WriteAllText(file, template); RunTestHelper(service => { var model = new { MyPrice = 0 }; string result = service.RunCompile(new LoadedTemplateSource(template, file), "key", null, model); Assert.AreEqual("free", result.Trim()); }, config => config.CachingProvider = cache); ICompiledTemplate compiledTemplate; Assert.IsTrue(cache.TryRetrieveTemplate(new NameOnlyTemplateKey("key", ResolveType.Global, null), null, out compiledTemplate)); // Contains line pragmas with the template file. Assert.IsTrue(compiledTemplate.CompilationData.SourceCode.Contains(file), ""); File.Delete(file); }
public void RazorEngineService_TestDebuggingWithHelper() { var cache = new DefaultCachingProvider(); var template = @"@helper Display(int price) { if (price == 0) { <text>free</text> } else { <text>@price</text> } } @Display(Model.MyPrice)"; RunTestHelper(service => { var model = new { MyPrice = 0 }; string result = service.RunCompile(template, "key", null, model); Assert.AreEqual("free", result.Trim()); }, config => config.CachingProvider = cache); ICompiledTemplate compiledTemplate; Assert.IsTrue(cache.TryRetrieveTemplate(new NameOnlyTemplateKey("key", ResolveType.Global, null), null, out compiledTemplate)); // #line hidden should be removed, so make debugging work, see https://github.com/Antaris/RazorEngine/issues/253. Assert.IsFalse(compiledTemplate.CompilationData.SourceCode.Contains("#line hidden")); }
private IRazorEngineService GetEngine() { if (_service == null) { _cache = new DefaultCachingProvider(); var config = new TemplateServiceConfiguration { BaseTemplateType = typeof(MixberryTemplate <>), Namespaces = new SortedSet <string>(new[] { "System", "System.Linq", "DotOrg.Mixberry.Web.Core.Templating", "DotOrg.Mixberry.Models", "DotOrg.Core.Helpers" }), AllowMissingPropertiesOnDynamic = true, CachingProvider = _cache, ReferenceResolver = new UseCurrentAssembliesReferenceResolver() //ReferenceResolver = new RazorEngineReferenceResolver() }; _service = RazorEngineService.Create(config); } return(_service); }
public void Roslyn_TestDisableTempFileLocking() { var cache = new DefaultCachingProvider(t => { }); var template = "@Model.Property"; RunTestHelper(service => { var model = new { Property = 0 }; string result = service.RunCompile(template, "key", null, model); Assert.AreEqual("0", result.Trim()); }, config => { config.Debug = false; config.CachingProvider = cache; config.DisableTempFileLocking = true; }); ICompiledTemplate compiledTemplate; Assert.IsTrue(cache.TryRetrieveTemplate(new NameOnlyTemplateKey("key", ResolveType.Global, null), null, out compiledTemplate)); var data = compiledTemplate.CompilationData; var folder = data.TmpFolder; Assert.IsTrue(Directory.Exists(folder)); data.DeleteAll(); Assert.IsFalse(Directory.Exists(folder)); }
public TemplateServiceConfiguration() { Activator = new DefaultActivator(); CompilerServiceFactory = #if RAZOR4 new Roslyn.RoslynCompilerServiceFactory(); #else new DefaultCompilerServiceFactory(); #endif EncodedStringFactory = new HtmlEncodedStringFactory(); #if !RAZOR4 #pragma warning disable 0618 // Backwards Compat. CodeInspectors = new List <ICodeInspector>(); #pragma warning restore 0618 // Backwards Compat. #endif ReferenceResolver = new UseCurrentAssembliesReferenceResolver(); CachingProvider = new DefaultCachingProvider(); TemplateManager = new DelegateTemplateManager(); Namespaces = new HashSet <string> { "System", "System.Collections.Generic", "System.Linq" }; var config = RazorEngineConfigurationSection.GetConfiguration(); Language = (config == null) ? Language.CSharp : config.DefaultLanguage; }
public TemplateServiceConfiguration() { Activator = new DefaultActivator(); CompilerServiceFactory = new DefaultCompilerServiceFactory(); EncodedStringFactory = new HtmlEncodedStringFactory(); #if !RAZOR4 #pragma warning disable 0618 // Backwards Compat. CodeInspectors = new List<ICodeInspector>(); #pragma warning restore 0618 // Backwards Compat. #endif ReferenceResolver = new UseCurrentAssembliesReferenceResolver(); CachingProvider = new DefaultCachingProvider(); TemplateManager = new DelegateTemplateManager(); Namespaces = new HashSet<string> { "System", "System.Collections.Generic", "System.Linq" }; var config = RazorEngineConfigurationSection.GetConfiguration(); Language = (config == null) ? Language.CSharp : config.DefaultLanguage; }
protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects). if (_proxy != null) { _proxy.Dispose(); _proxy = null; } } // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. // TODO: set large fields to null. disposedValue = true; } }
public RazorViewServiceConfiguration(Registration <IRazorViewLoader> razorViewLoaderRegistration) { if (razorViewLoaderRegistration == null) { throw new ArgumentNullException(nameof(razorViewLoaderRegistration)); } RazorViewLoader = razorViewLoaderRegistration; CompilerServiceFactory = new DefaultCompilerServiceFactory(); EncodedStringFactory = new HtmlEncodedStringFactory(); CachingProvider = new DefaultCachingProvider(); Namespaces = new HashSet <string>() { "System", "System.Collections.Generic", "System.Linq" }; RazorEngineConfigurationSection configuration = RazorEngineConfigurationSection.GetConfiguration(); Language = configuration?.DefaultLanguage ?? Language.CSharp; }
public TemplateServiceConfiguration() { Activator = new DefaultActivator(); CompilerServiceFactory = new Roslyn.RoslynCompilerServiceFactory(); EncodedStringFactory = new HtmlEncodedStringFactory(); ReferenceResolver = new UseCurrentAssembliesReferenceResolver(); CachingProvider = new DefaultCachingProvider(); TemplateManager = new DelegateTemplateManager(); Namespaces = new HashSet <string> { "System", "System.Collections.Generic", "System.Linq" }; Language = Language.CSharp; }
/// <summary> /// Check if everything is cleaned up. /// </summary> public string RazorEngineService_CleanUpWorks(CrossAppDomainCleanUp.IPrinter printer) { CrossAppDomainCleanUp.CurrentPrinter = printer; var cache = new DefaultCachingProvider(); RazorEngineServiceTestFixture.RunTestHelper(service => { string template = @"@Model.Name"; var result = service.RunCompile(template, "test", null, new { Name = "test" }); Assert.AreEqual("test", result); }, config => { config.CachingProvider = cache; }); ICompiledTemplate compiledTemplate; Assert.IsTrue(cache.TryRetrieveTemplate(new NameOnlyTemplateKey("test", ResolveType.Global, null), null, out compiledTemplate)); var folder = compiledTemplate.CompilationData.TmpFolder; Assert.IsTrue(Directory.Exists(folder)); return(folder); }
public PrecompiledCachingProvider() { _proxy = new DefaultCachingProvider(t => { }); }