/// <summary>
        /// Starts the Runtime host by creating an AppDomain and loading the runtime into it
        /// </summary>
        /// <param name="PhysicalPath">The physical disk path for the 'Web' directory where files are executed</param>
        /// <param name="VirtualPath">The name of the virtual path. Typically this will be "/" or the root path.</param>
        /// <param name="PrivateBinPath">The private bin path.</param>
        /// <param name="ConfigFile">The config file.</param>
        /// <returns></returns>
        public static wwAspRuntimeProxy Start(string PhysicalPath, string VirtualPath,
                                              string PrivateBinPath, string ConfigFile)
        {
            wwAspRuntimeProxy Host = wwAspRuntimeProxy.CreateApplicationHost(
                typeof(wwAspRuntimeProxy),
                VirtualPath, PhysicalPath, PrivateBinPath, ConfigFile);

            return(Host);
        }
        /// <summary>
        /// Unloads the runtime host by unloading the AppDomain. Use this to free memory if you are compiling lots of pages or recycle the host.
        /// </summary>
        /// <param name="Host">The host.</param>
        public static void Stop(wwAspRuntimeProxy Host)
        {
            if (Host != null)
            {
                Host.Context.Clear();
                Host.Context = null;

                Host.UnloadRuntime();

                AppDomain.Unload(Host.AppDomain);
                Host = null;
            }
        }
 /// <summary>
 /// Stops the ASP.Net runtime unloading the AppDomain
 /// </summary>
 /// <returns></returns>
 public bool Stop()
 {
     if (this.Proxy != null)
     {
         try
         {
             wwAspRuntimeProxy.Stop(this.Proxy);
             this.Proxy = null;
         }
         catch (Exception ex)
         {
             this.ErrorMessage = ex.Message;
             this.Error        = true;
             return(false);
         }
         return(true);
     }
     return(false);
 }
        /// <summary>
        /// Starts the ASP.Net runtime hosting by creating a new appdomain and loading the runtime into it.
        /// </summary>
        /// <returns>true or false</returns>
        public bool Start()
        {
            if (this.Proxy == null)
            {
                // *** Make sure ASP.Net registry keys exist
                // *** if IIS was never registered, required aspnet_isapi.dll
                // *** cannot be found otherwise
                this.GetInstallPathAndConfigureAspNetIfNeeded();

                if (this.VirtualPath.Length == 0 || this.PhysicalDirectory.Length == 0)
                {
                    this.ErrorMessage = "Virtual or Physical Path not set.";
                    this.Error        = true;
                    return(false);
                }

                // *** Force any assemblies assemblies to be copied
                this.MakeShadowCopies(this.ShadowCopyAssemblies);

                try
                {
                    this.Proxy = wwAspRuntimeProxy.Start(this.PhysicalDirectory, this.VirtualPath,
                                                         this.ApplicationBase, this.ConfigFile);

                    this.Proxy.PhysicalDirectory = this.PhysicalDirectory;
                    this.Proxy.VirtualPath       = this.VirtualPath;
                }
                catch (Exception ex)
                {
                    this.ErrorMessage = ex.Message;
                    this.Error        = true;
                    this.Proxy        = null;
                    return(false);
                }
                this.Cookies.Clear();
            }


            return(true);
        }
        /// <summary>
        /// Creates an instance of this class in the ASP.NET AppDomain
        /// </summary>
        /// <param name="hostType">Type of the application to be hosted. Essentially this class.</param>
        /// <param name="virtualDir">Name of the Virtual Directory that hosts this application. Not really used, other than on error messages and ASP Server Variable return values.</param>
        /// <param name="physicalDir">The physical location of the Virtual Directory for the application</param>
        /// <param name="PrivateBinPath">The private bin path.</param>
        /// <param name="ConfigurationFile">Location of the configuration file. Default to web.config in the bin directory.</param>
        /// <returns>
        /// object instance to the wwAspRuntimeProxy class you can call ProcessRequest on. Note this instance returned
        /// is a remoting proxy
        /// </returns>
        public static wwAspRuntimeProxy CreateApplicationHost(Type hostType, string virtualDir, string physicalDir,
                                                              string PrivateBinPath, string ConfigurationFile)
        {
            if (!(physicalDir.EndsWith("\\")))
            {
                physicalDir = physicalDir + "\\";
            }

            // *** Copy this hosting DLL into the /bin directory of the application
            string FileName = Assembly.GetExecutingAssembly().Location;

            try
            {
                if (!Directory.Exists(physicalDir + "bin\\"))
                {
                    Directory.CreateDirectory(physicalDir + "bin\\");
                }

                string JustFname = Path.GetFileName(FileName);
                File.Copy(FileName, physicalDir + "bin\\" + JustFname, true);
            }
            catch {; }

            wwAspRuntimeProxy Proxy = ApplicationHost.CreateApplicationHost(
                hostType,
                virtualDir,
                physicalDir)
                                      as wwAspRuntimeProxy;

            if (Proxy != null)
            {
                // *** Grab the AppDomain reference and add the ApplicationBase
                // *** Must call into the Proxy to do this
                Proxy.CaptureAppDomain();
            }


            return(Proxy);
        }