Exemple #1
0
        /// <summary>
        /// Finalizes (disposes) the request context.
        /// </summary>
        /// <remarks>
        /// Finalization comprises of the following actions (executed in the order):
        /// <list type="number">
        /// <term>Output buffers are flushed. This action may include calls to user defined filters (see <c>ob_start</c> function).</term>
        /// <term>Shutdown callbacks are invoked (if added by <c>register_shutdown_function</c> function).</term>
        /// <term>Session is closed. User defined session handling function may be invoked (see <c>session_set_save_handler</c> function).</term>
        /// <term>PHP objects are destroyed.</term>
        /// <term>HTTP Headers are flushed (if it wasn't done earlier).</term>
        /// <term>PHP resources are disposed.</term>
        /// <term>Per-request temporary files are deleted.</term>
        /// <term><see cref="RequestEnd"/> event is fired.</term>
        /// <term>Current request and script contexts are nulled.</term>
        /// </list>
        /// Multiple invocations of the method are ignored.
        /// Since session data need to be written to the session store (<c>HttpContext.Session</c>) this method has to be
        /// called before the ASP.NET session is ended for the request.
        /// </remarks>
        public void Dispose()
        {
            if (!disposed)
            {
                try
                {
                    scriptContext.GuardedCall <object, object>(scriptContext.ProcessShutdownCallbacks, null, false);

                    // Session is ended after destructing objects since PHP 5.0.5, use two-phase finalization:
                    scriptContext.GuardedCall <object, object>(scriptContext.FinalizePhpObjects, null, false);
                    scriptContext.GuardedCall <object, object>(scriptContext.FinalizeBufferedOutput, null, false);

                    TryDisposeBeforeFinalization();

                    // finalize objects created during session closing and output finalization:
                    scriptContext.GuardedCall <object, object>(scriptContext.FinalizePhpObjects, null, false);

                    // Platforms-specific dispose
                    TryDisposeAfterFinalization();
                }
                finally
                {
                    CleanUpResources();

                    // Platforms-specific finally dispose
                    FinallyDispose();

                    if (RequestEnd != null)
                    {
                        RequestEnd();
                    }

                    // remember the max capacity of dictionaries to preallocate next time:
                    if (scriptContext != null && scriptContext.MainScriptInfo != null)
                    {
                        scriptContext.MainScriptInfo.SaveMaxCounts(scriptContext);
                    }

                    // cleans this instance:
                    disposed                     = true;
                    this.scriptContext           = null;
                    ScriptContext.CurrentContext = null;

                    Debug.WriteLine("REQUEST", "-- disposed ----------------------");
                }
            }
        }
Exemple #2
0
        public static void RunApplication(Delegate /*!*/ mainRoutine, string relativeSourcePath, string sourceRoot)
        {
            bool is_pure = mainRoutine is RoutineDelegate;

            ApplicationContext app_context = ApplicationContext.Default;

            // default culture:
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            // try to preload configuration (to prevent exceptions during InitApplication:
            try
            {
                Configuration.Load(app_context);
            }
            catch (ConfigurationErrorsException e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            ApplicationConfiguration app_config = Configuration.Application;

            if (is_pure && !app_config.Compiler.LanguageFeaturesSet)
            {
                app_config.Compiler.LanguageFeatures = LanguageFeatures.PureModeDefault;
            }

            // environment settings; modifies the PATH variable to fix LoadLibrary called by native extensions:
            if (EnvironmentUtils.IsDotNetFramework)
            {
                string path = Environment.GetEnvironmentVariable("PATH");
                path = String.Concat(path, Path.PathSeparator, app_config.Paths.ExtNatives);

                Environment.SetEnvironmentVariable("PATH", path);
            }

            Type main_script;

            if (is_pure)
            {
                // loads the calling assembly:
                app_context.AssemblyLoader.Load(mainRoutine.Method.Module.Assembly, null);
                main_script = null;
            }
            else
            {
                main_script = mainRoutine.Method.DeclaringType;
                app_context.AssemblyLoader.LoadScriptLibrary(System.Reflection.Assembly.GetEntryAssembly(), ".");
            }

            using (ScriptContext context = InitApplication(app_context, main_script, relativeSourcePath, sourceRoot))
            {
                context.GuardedCall <object, object>(context.GuardedMain, mainRoutine, true);
            }
        }
        public static void RunApplication(Delegate /*!*/ mainRoutine, string relativeSourcePath, string sourceRoot)
        {
            bool is_pure = mainRoutine is RoutineDelegate;

            ApplicationContext app_context = ApplicationContext.Default;

            // default culture:
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            // try to preload configuration (to prevent exceptions during InitApplication:
            try
            {
                Configuration.Load(app_context);
            }
            catch (ConfigurationErrorsException e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            ApplicationConfiguration app_config = Configuration.Application;

            if (is_pure && !app_config.Compiler.LanguageFeaturesSet)
            {
                app_config.Compiler.LanguageFeatures = LanguageFeatures.PureModeDefault;
            }

            Type main_script;

            if (is_pure)
            {
                // loads the calling assembly:
                app_context.AssemblyLoader.Load(mainRoutine.Method.Module.Assembly, null);
                main_script = null;
            }
            else
            {
                main_script = mainRoutine.Method.DeclaringType;
                app_context.AssemblyLoader.LoadScriptLibrary(System.Reflection.Assembly.GetEntryAssembly(), ".");
            }

            using (ScriptContext context = InitApplication(app_context, main_script, relativeSourcePath, sourceRoot))
            {
                context.GuardedCall <object, object>(context.GuardedMain, mainRoutine, true);
            }
        }