diff --git a/src/Umbraco.Core/CoreBootManager.cs b/src/Umbraco.Core/CoreBootManager.cs index 9443a8abce..534c523e15 100644 --- a/src/Umbraco.Core/CoreBootManager.cs +++ b/src/Umbraco.Core/CoreBootManager.cs @@ -110,15 +110,15 @@ namespace Umbraco.Core if (_isStarted) throw new InvalidOperationException("The boot manager has already been initialized"); - if (afterStartup != null) - { - afterStartup(ApplicationContext.Current); - } - //call OnApplicationStarting of each application events handler ApplicationEventsResolver.Current.ApplicationEventHandlers .ForEach(x => x.OnApplicationStarting(UmbracoApplication, ApplicationContext)); + if (afterStartup != null) + { + afterStartup(ApplicationContext.Current); + } + _isStarted = true; return this; @@ -140,15 +140,18 @@ namespace Umbraco.Core //stop the timer and log the output _timer.Dispose(); - if (afterComplete != null) - { - afterComplete(ApplicationContext.Current); - } - //call OnApplicationStarting of each application events handler ApplicationEventsResolver.Current.ApplicationEventHandlers .ForEach(x => x.OnApplicationStarted(UmbracoApplication, ApplicationContext)); + //Now, startup all of our legacy startup handler + ApplicationEventsResolver.Current.InstantiateLegacyStartupHanlders(); + + if (afterComplete != null) + { + afterComplete(ApplicationContext.Current); + } + _isComplete = true; // we're ready to serve content! diff --git a/src/Umbraco.Core/ObjectResolution/ApplicationEventsResolver.cs b/src/Umbraco.Core/ObjectResolution/ApplicationEventsResolver.cs index 7f5b6f6365..c706740861 100644 --- a/src/Umbraco.Core/ObjectResolution/ApplicationEventsResolver.cs +++ b/src/Umbraco.Core/ObjectResolution/ApplicationEventsResolver.cs @@ -8,27 +8,49 @@ namespace Umbraco.Core.ObjectResolution /// /// A resolver to return all IApplicationEvents objects /// - internal sealed class ApplicationEventsResolver : ManyObjectsResolverBase + internal sealed class ApplicationEventsResolver : ManyObjectsResolverBase { - /// + private readonly LegacyStartupHandlerResolver _legacyResolver; + + /// /// Constructor /// /// internal ApplicationEventsResolver(IEnumerable applicationEventHandlers) : base(applicationEventHandlers) { - + //create the legacy resolver and only include the legacy types + _legacyResolver = new LegacyStartupHandlerResolver( + applicationEventHandlers.Where(x => !TypeHelper.IsTypeAssignableFrom(x))); } - /// + /// + /// Override in order to only return types of IApplicationEventHandler and above, + /// do not include the legacy types of IApplicationStartupHandler + /// + protected override IEnumerable InstanceTypes + { + get { return base.InstanceTypes.Where(TypeHelper.IsTypeAssignableFrom); } + } + + /// /// Gets the implementations. /// public IEnumerable ApplicationEventHandlers { - get { return Values.OfType(); } + get { return Values; } } + /// + /// Create instances of all of the legacy startup handlers + /// + public void InstantiateLegacyStartupHanlders() + { + //this will instantiate them all + var handlers = _legacyResolver.LegacyStartupHandlers; + } + protected override bool SupportsClear { get { return false; } @@ -44,5 +66,19 @@ namespace Umbraco.Core.ObjectResolution get { return false; } } + private class LegacyStartupHandlerResolver : ManyObjectsResolverBase + { + internal LegacyStartupHandlerResolver(IEnumerable legacyStartupHandlers) + : base(legacyStartupHandlers) + { + + } + + public IEnumerable LegacyStartupHandlers + { + get { return Values; } + } + } + } } \ No newline at end of file diff --git a/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs b/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs index 067c30aeb3..0227e68908 100644 --- a/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs +++ b/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs @@ -6,6 +6,7 @@ using System.Web; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.ObjectResolution; +using umbraco.interfaces; namespace Umbraco.Tests.BootManagers { @@ -53,12 +54,14 @@ namespace Umbraco.Tests.BootManagers protected override void InitializeApplicationEventsResolver() { //create an empty resolver so we can add our own custom ones (don't type find) - ApplicationEventsResolver.Current = new ApplicationEventsResolver( - Enumerable.Empty()) + ApplicationEventsResolver.Current = new ApplicationEventsResolver(new Type[] + { + typeof(LegacyStartupHandler), + typeof(TestApplicationEventHandler) + }) { CanResolveBeforeFrozen = true }; - ApplicationEventsResolver.Current.AddType(); } protected override void InitializeResolvers() @@ -67,6 +70,19 @@ namespace Umbraco.Tests.BootManagers } } + /// + /// Test legacy startup handler + /// + public class LegacyStartupHandler : IApplicationStartupHandler + { + public static bool Initialized = false; + + public LegacyStartupHandler() + { + Initialized = true; + } + } + /// /// test event handler /// @@ -102,5 +118,29 @@ namespace Umbraco.Tests.BootManagers Assert.IsTrue(TestApplicationEventHandler.Started); } + [Test] + public void Ensure_Legacy_Startup_Handlers_Not_Started_Until_Complete() + { + EventHandler starting = (sender, args) => + { + Assert.IsTrue(TestApplicationEventHandler.Initialized); + Assert.IsTrue(TestApplicationEventHandler.Starting); + Assert.IsFalse(LegacyStartupHandler.Initialized); + }; + EventHandler started = (sender, args) => + { + Assert.IsTrue(TestApplicationEventHandler.Started); + Assert.IsTrue(LegacyStartupHandler.Initialized); + }; + TestApp.ApplicationStarting += starting; + TestApp.ApplicationStarted += started; + + _testApp.StartApplication(_testApp, new EventArgs()); + + TestApp.ApplicationStarting -= starting; + TestApp.ApplicationStarting -= started; + + } + } }