/// <summary> /// Call to start the Host running. Follow by a calls to RenderTemplate to /// render individual templates. Call Stop when done. /// </summary> /// <returns>true or false - check ErrorMessage on false </returns> public virtual bool Start() { if (Engine == null) { if (UseAppDomain) { Engine = RazorEngineFactory <TBaseTemplateType> .CreateRazorHostInAppDomain(); } else { Engine = RazorEngineFactory <TBaseTemplateType> .CreateRazorHost(); } if (Engine == null) { return(false); } Engine.HostContainer = this; Engine.ReferencedNamespaces.AddRange(ReferencedNamespaces); Engine.Configuration = Configuration; if (Engine == null) { this.ErrorMessage = EngineFactory.ErrorMessage; return(false); } } return(true); }
/// <summary> /// Stops the Host and releases the host AppDomain and cached /// assemblies. /// </summary> /// <returns>true or false</returns> public bool Stop() { //this.LoadedAssemblies.Clear(); RazorEngineFactory <RazorTemplateBase> .UnloadRazorHostInAppDomain(); this.Engine = null; return(true); }
/// <summary> /// Routine that returns an instance of the RazorHost hosted /// in a separate AppDomain. Checks for existance of the host /// and creates only if needed. You need to cache the host /// to avoid excessive resource use. /// </summary> /// <returns></returns> private RazorEngine <RazorTemplateBase> CreateHost() { if (this.Host != null) { return(this.Host); } // Use Static Methods - no error message if host doesn't load this.Host = RazorEngineFactory <RazorTemplateBase> .CreateRazorHost(); //this.Host = RazorEngineFactory<RazorTemplateBase>.CreateRazorHostInAppDomain(); if (this.Host == null) { System.Windows.Forms.MessageBox.Show("Unable to load Razor Template Host", "Razor Hosting", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } return(this.Host); }
public void SimplestRazorEngineTestWithAppDomain() { string template = @"Hello World @Model.Name. Time is: @DateTime.Now"; // Load engine into new AppDomain var host = RazorEngineFactory <RazorTemplateBase> .CreateRazorHostInAppDomain(); // Note: You can't use anonymouse types for cross-AppDomain calls // Models passed must inherit MarshalByRefObject or be [Serializable] string result = host.RenderTemplate(template, new Person { Name = "Joe Doe" }); Assert.IsNotNull(result, host.ErrorMessage); Assert.IsTrue(result.Contains("Joe Doe")); // shut down AppDomain RazorEngineFactory <RazorTemplateBase> .UnloadRazorHostInAppDomain(); Console.WriteLine(result); }