Moving Warmup code to a different assembly

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2011-03-23 17:20:38 -07:00
parent 04e9751725
commit b165807cf3
8 changed files with 185 additions and 59 deletions

View File

@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Orchard.Startup</RootNamespace>
<AssemblyName>Orchard.Startup</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac">
<HintPath>..\..\lib\autofac\Autofac.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Web" />
<Reference Include="System.Data.DataSetExtensions" />
</ItemGroup>
<ItemGroup>
<Compile Include="Starter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WarmupHttpModule.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Orchard\Orchard.Framework.csproj">
<Project>{2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6}</Project>
<Name>Orchard.Framework</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -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")]

View File

@@ -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<ContainerBuilder> 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();
}
}
/// <summary>
/// Initializes Orchard's Host in a separate thread
/// </summary>
public static void LaunchStartupThread(Action<ContainerBuilder> 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();
}
});
}
}
}

View File

@@ -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();
}

View File

@@ -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();
}
/// <summary>
/// Initializes Orchard's Host in a separate thread
/// </summary>
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();
}
});
}
}
}

View File

@@ -127,7 +127,6 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="WarmupHttpModule.cs" />
<Content Include="Config\log4net.config" />
<Content Include="Config\Sample.HostComponents.config">
<SubType>Designer</SubType>
@@ -144,6 +143,10 @@
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Orchard.Startup\Orchard.Startup.csproj">
<Project>{5C0D5249-AEF5-4BB6-8F5F-057B91AC2D7A}</Project>
<Name>Orchard.Startup</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard\Orchard.Framework.csproj">
<Project>{2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6}</Project>
<Name>Orchard.Framework</Name>

View File

@@ -122,7 +122,7 @@
</httpHandlers>
<httpModules>
<add name="WarmupHttpModule" type="Orchard.Web.WarmupHttpModule, Orchard.Web"/>
<add name="WarmupHttpModule" type="Orchard.Startup.WarmupHttpModule, Orchard.Startup"/>
</httpModules>
</system.web>
<!--
@@ -134,7 +134,7 @@
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="WarmupHttpModule" />
<add name="WarmupHttpModule" type="Orchard.Web.WarmupHttpModule, Orchard.Web"/>
<add name="WarmupHttpModule" type="Orchard.Startup.WarmupHttpModule, Orchard.Startup"/>
</modules>
<handlers accessPolicy="Script">
<!-- clear all handlers, prevents executing code file extensions, prevents returning any file contents -->

View File

@@ -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