static void Process(HttpWorkerRequest req) { bool error = false; if (firstRun) { firstRun = false; if (initialException != null) { FinishWithException(req, HttpException.NewWithCode("Initial exception", initialException, WebEventCodes.RuntimeErrorRequestAbort)); error = true; } SetupOfflineWatch(); } HttpContext context = new HttpContext(req); HttpContext.Current = context; if (AppIsOffline(context)) { return; } // // Get application instance (create or reuse an instance of the correct class) // HttpApplication app = null; if (!error) { try { app = HttpApplicationFactory.GetApplication(context); } catch (Exception e) { FinishWithException(req, HttpException.NewWithCode(String.Empty, e, WebEventCodes.RuntimeErrorRequestAbort)); error = true; } } if (error) { context.Request.ReleaseResources(); context.Response.ReleaseResources(); HttpContext.Current = null; } else { context.ApplicationInstance = app; req.SetEndOfSendNotification(end_of_send_cb, context); // // Ask application to service the request // IHttpHandler ihh = app; // IAsyncResult appiar = ihah.BeginProcessRequest (context, new AsyncCallback (request_processed), context); // ihah.EndProcessRequest (appiar); ihh.ProcessRequest(context); HttpApplicationFactory.Recycle(app); } }
internal static void Recycle(HttpApplication app) { bool dispose = false; HttpApplicationFactory factory = theFactory; if (Interlocked.CompareExchange(ref factory.next_free, app, null) == null) { return; } lock (factory.available) { if (factory.available.Count < 64) { factory.available.Push(app); } else { dispose = true; } } if (dispose) { app.Dispose(); } }
internal static void ExecuteLocalRequestAndCaptureResponse(string path, TextWriter writer, ErrorFormatterGenerator errorFormatterGenerator) { HttpRequest request = new HttpRequest(VirtualPath.CreateAbsolute(path), string.Empty); HttpResponse response = new HttpResponse(writer); HttpContext context = new HttpContext(request, response); HttpApplication applicationInstance = HttpApplicationFactory.GetApplicationInstance(context) as HttpApplication; context.ApplicationInstance = applicationInstance; try { context.Server.Execute(path); } catch (HttpException exception) { if (errorFormatterGenerator != null) { context.Response.SetOverrideErrorFormatter(errorFormatterGenerator.GetErrorFormatter(exception)); } context.Response.ReportRuntimeError(exception, false, true); } finally { if (applicationInstance != null) { context.ApplicationInstance = null; HttpApplicationFactory.RecycleApplicationInstance(applicationInstance); } } }
// // Shuts down the AppDomain // static void ShutdownAppDomain(object args) { queue_manager.Dispose(); // This will call Session_End if needed. InternalCache.InvokePrivateCallbacks(); // Kill our application. HttpApplicationFactory.Dispose(); ThreadPool.QueueUserWorkItem(new WaitCallback(DoUnload), null); }
private void TimerCompletionCallback(object state) { HttpApplicationFactory.TrimApplicationInstances(); if (((((this._idleTimeout != TimeSpan.MaxValue) && !HostingEnvironment.ShutdownInitiated) && (HostingEnvironment.BusyCount == 0)) && (DateTime.UtcNow > this.LastEvent.Add(this._idleTimeout))) && !Debugger.IsAttached) { HttpRuntime.SetShutdownReason(ApplicationShutdownReason.IdleTimeout, System.Web.SR.GetString("Hosting_Env_IdleTimeout")); HostingEnvironment.InitiateShutdownWithoutDemand(); } }
internal static void AttachEvents(HttpApplication app) { HttpApplicationFactory factory = theFactory; Hashtable possibleEvents = factory.GetApplicationTypeEvents(app); foreach (string key in possibleEvents.Keys) { int pos = key.IndexOf('_'); string moduleName = key.Substring(0, pos); object target; if (moduleName == "Application") { target = app; } else { target = app.Modules [moduleName]; if (target == null) { continue; } } string eventName = key.Substring(pos + 1); EventInfo evt = target.GetType().GetEvent(eventName); if (evt == null) { continue; } string usualName = moduleName + "_" + eventName; object methodData = possibleEvents [usualName]; if (methodData == null) { continue; } if (eventName == "End" && moduleName == "Session") { Interlocked.CompareExchange(ref factory.session_end, methodData, null); continue; } if (methodData is MethodInfo) { factory.AddHandler(evt, target, app, (MethodInfo)methodData); continue; } ArrayList list = (ArrayList)methodData; foreach (MethodInfo method in list) { factory.AddHandler(evt, target, app, method); } } }
// // Multiple-threads might hit this one on startup, and we have // to delay-initialize until we have the HttpContext // internal static HttpApplication GetApplication(HttpContext context) { HttpApplicationFactory factory = theFactory; HttpApplication app = null; if (factory.app_start_needed) { if (context == null) { return(null); } factory.InitType(context); lock (factory) { if (factory.app_start_needed) { foreach (string dir in HttpApplication.BinDirs) { WatchLocationForRestart(dir, "*.dll"); } // Restart if the App_* directories are created... WatchLocationForRestart(".", "App_Code"); WatchLocationForRestart(".", "App_Browsers"); WatchLocationForRestart(".", "App_GlobalResources"); // ...or their contents is changed. WatchLocationForRestart("App_Code", "*", true); WatchLocationForRestart("App_Browsers", "*"); WatchLocationForRestart("App_GlobalResources", "*"); app = factory.FireOnAppStart(context); factory.app_start_needed = false; return(app); } } } app = (HttpApplication)Interlocked.Exchange(ref factory.next_free, null); if (app != null) { app.RequestCompleted = false; return(app); } lock (factory.available) { if (factory.available.Count > 0) { app = (HttpApplication)factory.available.Pop(); app.RequestCompleted = false; return(app); } } return((HttpApplication)Activator.CreateInstance(factory.app_type, true)); }
// // Shuts down the AppDomain // static void ShutdownAppDomain() { queue_manager.Dispose(); // This will call Session_End if needed. InternalCache.InvokePrivateCallbacks(); // Kill our application. HttpApplicationFactory.Dispose(); ThreadPool.QueueUserWorkItem(delegate { try { DoUnload(); } catch { } }); }
public static void UnloadAppDomain() { // // TODO: call ReleaseResources // domainUnloading = true; HttpApplicationFactory.DisableWatchers(); ThreadPool.QueueUserWorkItem(delegate { try { ShutdownAppDomain(); } catch (Exception e) { Console.Error.WriteLine(e); } }); }
// The lock is in InvokeSessionEnd static HttpApplication GetApplicationForSessionEnd() { HttpApplicationFactory factory = theFactory; if (factory.available_for_end.Count > 0) { return((HttpApplication)factory.available_for_end.Pop()); } HttpApplication app = (HttpApplication)Activator.CreateInstance(factory.app_type, true); app.InitOnce(false); return(app); }
static void SetOfflineMode(bool offline, string filePath) { if (!offline) { app_offline_file = null; if (HttpApplicationFactory.ApplicationDisabled) { HttpRuntime.UnloadAppDomain(); } } else { app_offline_file = filePath; HttpApplicationFactory.DisableWatchers(); HttpApplicationFactory.ApplicationDisabled = true; InternalCache.InvokePrivateCallbacks(); HttpApplicationFactory.Dispose(); } }
internal static void RecycleForSessionEnd(HttpApplication app) { bool dispose = false; HttpApplicationFactory factory = theFactory; lock (factory.available_for_end) { if (factory.available_for_end.Count < 64) { factory.available_for_end.Push(app); } else { dispose = true; } } if (dispose) { app.Dispose(); } }
private void TimerCompletionCallback(Object state) { // user idle timer to trim the free list of app instanced HttpApplicationFactory.TrimApplicationInstances(); // no idle timeout if (_idleTimeout == TimeSpan.MaxValue) { return; } // don't do idle timeout if already shutting down if (HostingEnvironment.ShutdownInitiated) { return; } // check if there are active requests if (HostingEnvironment.BusyCount != 0) { return; } // check if enough time passed if (DateTime.UtcNow <= LastEvent.Add(_idleTimeout)) { return; } // check if debugger is attached if (System.Diagnostics.Debugger.IsAttached) { return; } // shutdown HttpRuntime.SetShutdownReason(ApplicationShutdownReason.IdleTimeout, SR.GetString(SR.Hosting_Env_IdleTimeout)); HostingEnvironment.InitiateShutdownWithoutDemand(); }
internal static void InvokeSessionEnd(object state, object source, EventArgs e) { HttpApplicationFactory factory = theFactory; MethodInfo method = null; HttpApplication app = null; lock (factory.available_for_end) { method = (MethodInfo)factory.session_end; if (method == null) { return; } app = GetApplicationForSessionEnd(); } app.SetSession((HttpSessionState)state); try { method.Invoke(app, new object [] { (source == null ? app : source), e }); } catch (Exception) { // Ignore } RecycleForSessionEnd(app); }
static void Process(HttpWorkerRequest req) { #if TARGET_J2EE HttpContext context = HttpContext.Current; if (context == null) { context = new HttpContext(req); } else { context.SetWorkerRequest(req); } #else HttpContext context = new HttpContext(req); #endif HttpContext.Current = context; bool error = false; #if !TARGET_J2EE if (firstRun) { #if NET_2_0 SetupOfflineWatch(); #endif firstRun = false; if (initialException != null) { FinishWithException(req, new HttpException("Initial exception", initialException)); error = true; } } #if NET_2_0 if (AppIsOffline(context)) { return; } #endif #endif // // Get application instance (create or reuse an instance of the correct class) // HttpApplication app = null; if (!error) { try { app = HttpApplicationFactory.GetApplication(context); } catch (Exception e) { FinishWithException(req, new HttpException("", e)); error = true; } } if (error) { context.Request.ReleaseResources(); context.Response.ReleaseResources(); HttpContext.Current = null; } else { context.ApplicationInstance = app; req.SetEndOfSendNotification(end_of_send_cb, context); // // Ask application to service the request // #if TARGET_J2EE IHttpAsyncHandler ihah = app; if (context.Handler == null) { ihah.BeginProcessRequest(context, new AsyncCallback(request_processed), context); } else { app.Tick(); } //ihh.ProcessRequest (context); IHttpExtendedHandler extHandler = context.Handler as IHttpExtendedHandler; if (extHandler != null && !extHandler.IsCompleted) { return; } if (context.Error is UnifyRequestException) { return; } ihah.EndProcessRequest(null); #else IHttpHandler ihh = app; // IAsyncResult appiar = ihah.BeginProcessRequest (context, new AsyncCallback (request_processed), context); // ihah.EndProcessRequest (appiar); ihh.ProcessRequest(context); #endif HttpApplicationFactory.Recycle(app); } }