From b165807cf397be8d22205b309d49da5c04a01fa9 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Wed, 23 Mar 2011 17:20:38 -0700 Subject: [PATCH] Moving Warmup code to a different assembly --HG-- branch : dev --- src/Orchard.Startup/Orchard.Startup.csproj | 60 ++++++++++++++++++ .../Properties/AssemblyInfo.cs | 35 +++++++++++ src/Orchard.Startup/Starter.cs | 63 +++++++++++++++++++ .../WarmupHttpModule.cs | 4 +- src/Orchard.Web/Global.asax.cs | 61 +++--------------- src/Orchard.Web/Orchard.Web.csproj | 5 +- src/Orchard.Web/Web.config | 4 +- src/Orchard.sln | 12 ++++ 8 files changed, 185 insertions(+), 59 deletions(-) create mode 100644 src/Orchard.Startup/Orchard.Startup.csproj create mode 100644 src/Orchard.Startup/Properties/AssemblyInfo.cs create mode 100644 src/Orchard.Startup/Starter.cs rename src/{Orchard.Web => Orchard.Startup}/WarmupHttpModule.cs (95%) diff --git a/src/Orchard.Startup/Orchard.Startup.csproj b/src/Orchard.Startup/Orchard.Startup.csproj new file mode 100644 index 000000000..9bf053498 --- /dev/null +++ b/src/Orchard.Startup/Orchard.Startup.csproj @@ -0,0 +1,60 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A} + Library + Properties + Orchard.Startup + Orchard.Startup + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\lib\autofac\Autofac.dll + + + + + + + + + + + + + {2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6} + Orchard.Framework + + + + + \ No newline at end of file diff --git a/src/Orchard.Startup/Properties/AssemblyInfo.cs b/src/Orchard.Startup/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..7da469bba --- /dev/null +++ b/src/Orchard.Startup/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Orchard.Starter")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyProduct("Orchard")] +[assembly: AssemblyCopyright("Copyright © Outercurve Foundation 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("02319f2f-22ec-448d-b4f2-568fb8502242")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.20")] +[assembly: AssemblyFileVersion("1.0.20")] diff --git a/src/Orchard.Startup/Starter.cs b/src/Orchard.Startup/Starter.cs new file mode 100644 index 000000000..3c090bc3f --- /dev/null +++ b/src/Orchard.Startup/Starter.cs @@ -0,0 +1,63 @@ +using System; +using System.Threading; +using System.Web; +using Autofac; +using Orchard.Environment; + +namespace Orchard.Startup { + public class Starter { + private static IOrchardHost _host; + private static Exception _error; + + public static void OnBeginRequest(HttpContext context, Action registrations) { + if (_error != null) { + // Host startup resulted in an error + + // Throw error once, and restart launch; machine state may have changed + // so we need to simulate a "restart". + var error = _error; + LaunchStartupThread(registrations); + throw error; + } + + // Only notify if the host has started up + if (_host == null) { + return; + } + + context.Items["originalHttpContext"] = context; + _host.BeginRequest(); + } + + public static void OnEndRequest() { + // Only notify if the host has started up + if (_host != null) { + _host.EndRequest(); + } + } + + /// + /// Initializes Orchard's Host in a separate thread + /// + public static void LaunchStartupThread(Action registrations) { + _host = null; + _error = null; + + ThreadPool.QueueUserWorkItem( + state => { + try { + var host = OrchardStarter.CreateHost(registrations); + host.Initialize(); + _host = host; + } + catch (Exception e) { + _error = e; + } + finally { + // Execute pending actions as the host is available + WarmupHttpModule.Signal(); + } + }); + } + } +} diff --git a/src/Orchard.Web/WarmupHttpModule.cs b/src/Orchard.Startup/WarmupHttpModule.cs similarity index 95% rename from src/Orchard.Web/WarmupHttpModule.cs rename to src/Orchard.Startup/WarmupHttpModule.cs index 7f4acac6d..89e6bea35 100644 --- a/src/Orchard.Web/WarmupHttpModule.cs +++ b/src/Orchard.Startup/WarmupHttpModule.cs @@ -6,7 +6,7 @@ using System.Threading; using System.Web; using System.Web.Hosting; -namespace Orchard.Web { +namespace Orchard.Startup { public class WarmupHttpModule : IHttpModule { private const string WarmupFilesPath = "~/App_Data/Warmup/"; private HttpApplication _context; @@ -65,7 +65,7 @@ namespace Orchard.Web { return asyncResult; } - private void EndBeginRequest(IAsyncResult ar) { + private static void EndBeginRequest(IAsyncResult ar) { ((WarmupAsyncResult)ar).Wait(); } diff --git a/src/Orchard.Web/Global.asax.cs b/src/Orchard.Web/Global.asax.cs index 3a6c20533..5340b164e 100644 --- a/src/Orchard.Web/Global.asax.cs +++ b/src/Orchard.Web/Global.asax.cs @@ -1,52 +1,31 @@ -using System; -using System.Threading; -using System.Web; +using System.Web; using System.Web.Mvc; using System.Web.Routing; using Autofac; -using Orchard.Environment; +using Orchard.Startup; namespace Orchard.Web { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : HttpApplication { - private static IOrchardHost _host; - private static Exception _error; public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); } protected void Application_Start() { - LaunchStartupThread(); + RegisterRoutes(RouteTable.Routes); + + Starter.LaunchStartupThread(MvcSingletons); } protected void Application_BeginRequest() { - if (_error != null) { - // Host startup resulted in an error - - // Throw error once, and restart launch; machine state may have changed - // so we need to simulate a "restart". - var error = _error; - LaunchStartupThread(); - throw error; - } - - // Only notify if the host has started up - if (_host == null) { - return; - } - - Context.Items["originalHttpContext"] = Context; - _host.BeginRequest(); + Starter.OnBeginRequest(Context, MvcSingletons); } protected void Application_EndRequest() { - // Only notify if the host has started up - if (_host != null) { - _host.EndRequest(); - } + Starter.OnEndRequest(); } static void MvcSingletons(ContainerBuilder builder) { @@ -54,31 +33,5 @@ namespace Orchard.Web { builder.Register(ctx => ModelBinders.Binders).SingleInstance(); builder.Register(ctx => ViewEngines.Engines).SingleInstance(); } - - /// - /// Initializes Orchard's Host in a separate thread - /// - private static void LaunchStartupThread() { - RegisterRoutes(RouteTable.Routes); - - _host = null; - _error = null; - - ThreadPool.QueueUserWorkItem( - state => { - try { - var host = OrchardStarter.CreateHost(MvcSingletons); - host.Initialize(); - _host = host; - } - catch (Exception e) { - _error = e; - } - finally { - // Execute pending actions as the host is available - WarmupHttpModule.Signal(); - } - }); - } } } diff --git a/src/Orchard.Web/Orchard.Web.csproj b/src/Orchard.Web/Orchard.Web.csproj index 783a5c721..5f6286d6f 100644 --- a/src/Orchard.Web/Orchard.Web.csproj +++ b/src/Orchard.Web/Orchard.Web.csproj @@ -127,7 +127,6 @@ Global.asax - Designer @@ -144,6 +143,10 @@ + + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A} + Orchard.Startup + {2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6} Orchard.Framework diff --git a/src/Orchard.Web/Web.config b/src/Orchard.Web/Web.config index 498046275..ef697fa7a 100644 --- a/src/Orchard.Web/Web.config +++ b/src/Orchard.Web/Web.config @@ -122,7 +122,7 @@ - + diff --git a/src/Orchard.sln b/src/Orchard.sln index 57da0789e..d3a5208d8 100644 --- a/src/Orchard.sln +++ b/src/Orchard.sln @@ -114,6 +114,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchard.ImportExport", "Orc EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchard.Warmup", "Orchard.Web\Modules\Orchard.WarmUp\Orchard.Warmup.csproj", "{9CD5C81F-5828-4384-8474-2E2BE71D5EDD}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchard.Startup", "Orchard.Startup\Orchard.Startup.csproj", "{5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution CodeCoverage|Any CPU = CodeCoverage|Any CPU @@ -604,6 +606,16 @@ Global {9CD5C81F-5828-4384-8474-2E2BE71D5EDD}.FxCop|Any CPU.ActiveCfg = Release|Any CPU {9CD5C81F-5828-4384-8474-2E2BE71D5EDD}.Release|Any CPU.ActiveCfg = Release|Any CPU {9CD5C81F-5828-4384-8474-2E2BE71D5EDD}.Release|Any CPU.Build.0 = Release|Any CPU + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}.CodeCoverage|Any CPU.ActiveCfg = Release|Any CPU + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}.CodeCoverage|Any CPU.Build.0 = Release|Any CPU + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}.Coverage|Any CPU.ActiveCfg = Release|Any CPU + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}.Coverage|Any CPU.Build.0 = Release|Any CPU + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}.FxCop|Any CPU.ActiveCfg = Release|Any CPU + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}.FxCop|Any CPU.Build.0 = Release|Any CPU + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE