Exemple #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(CodeProvider);
                }

                if (Engine == null)
                {
                    SetError(RazorEngineFactory <TBaseTemplateType> .Current.ErrorMessage);
                    return(false);
                }

                Engine.HostContainer = this;

                foreach (string Namespace in ReferencedNamespaces)
                {
                    Engine.AddNamespace(Namespace);
                }

                foreach (string assembly in ReferencedAssemblies)
                {
                    Engine.AddAssembly(assembly);
                }

                Engine.Configuration = Configuration;
            }

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Instance method that creates a RazorHost in a new AppDomain.
        /// This method requires that you keep the Factory around in
        /// order to keep the AppDomain alive and be able to unload it.
        /// </summary>
        /// <param name="codeProvider"></param>
        /// <returns></returns>
        public RazorEngine <TBaseTemplateType> GetRazorHostInAppDomain()
        {
            LocalAppDomain = CreateAppDomain(null);
            if (LocalAppDomain == null)
            {
                return(null);
            }

            /// Create the instance inside of the new AppDomain
            /// Note: remote domain uses local EXE's AppBasePath!!!
            RazorEngine <TBaseTemplateType> host = null;

            try
            {
                Assembly ass          = Assembly.GetExecutingAssembly();
                string   AssemblyPath = ass.Location;

                var templateType = typeof(RazorEngine <TBaseTemplateType>);

                object instance = LocalAppDomain.CreateInstanceFrom(AssemblyPath, templateType.FullName, false, BindingFlags.Default, null,
                                                                    new object [] { null }, CultureInfo.CurrentCulture, null)
                                  .Unwrap();

                host = instance as RazorEngine <TBaseTemplateType>;
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                return(null);
            }

            Assembly[] assemblies = LocalAppDomain.GetAssemblies();
            var        count      = assemblies.Length;

            return(host);
        }