Exemplo n.º 1
0
        /// <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);
        }
        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);
        }