From 352e02f064032f1ffc288fa38857135667965a1e Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 24 Aug 2016 12:28:31 +0200 Subject: [PATCH] Resvolution - ServerRegistrarResolver --- src/Umbraco.Core/ApplicationContext.cs | 3 +- src/Umbraco.Core/CoreBootManager.cs | 4 +- .../DependencyInjection/Current.cs | 3 ++ src/Umbraco.Core/Sync/ApplicationUrlHelper.cs | 4 +- .../Sync/ServerRegistrarResolver.cs | 49 ------------------- src/Umbraco.Core/Umbraco.Core.csproj | 1 - .../ApplicationUrlHelperTests.cs | 31 ++++++------ .../DistributedCache/DistributedCacheTests.cs | 5 +- .../Integration/ContentEventsTests.cs | 3 +- src/Umbraco.Web/Cache/DistributedCache.cs | 20 ++++---- src/Umbraco.Web/Current.cs | 3 ++ .../ServerRegistrationEventHandler.cs | 2 +- 12 files changed, 39 insertions(+), 89 deletions(-) delete mode 100644 src/Umbraco.Core/Sync/ServerRegistrarResolver.cs diff --git a/src/Umbraco.Core/ApplicationContext.cs b/src/Umbraco.Core/ApplicationContext.cs index c8b2e3325c..3dbc68638a 100644 --- a/src/Umbraco.Core/ApplicationContext.cs +++ b/src/Umbraco.Core/ApplicationContext.cs @@ -323,8 +323,7 @@ namespace Umbraco.Core /// internal ServerRole GetCurrentServerRole() { - var registrar = ServerRegistrarResolver.Current.Registrar; - return registrar.GetCurrentServerRole(); + return DependencyInjection.Current.ServerRegistrar.GetCurrentServerRole(); } /// diff --git a/src/Umbraco.Core/CoreBootManager.cs b/src/Umbraco.Core/CoreBootManager.cs index fa542fffa3..d3e68ad0e1 100644 --- a/src/Umbraco.Core/CoreBootManager.cs +++ b/src/Umbraco.Core/CoreBootManager.cs @@ -420,11 +420,11 @@ namespace Umbraco.Core // dist calls enabled, in which case we'll use the config server registrar if (UmbracoConfig.For.UmbracoSettings().DistributedCall.Enabled) { - ServerRegistrarResolver.Current = new ServerRegistrarResolver(new ConfigServerRegistrar(UmbracoConfig.For.UmbracoSettings())); + Container.RegisterSingleton(_ => new ConfigServerRegistrar(UmbracoConfig.For.UmbracoSettings())); } else { - ServerRegistrarResolver.Current = new ServerRegistrarResolver( + Container.RegisterSingleton(_ => new DatabaseServerRegistrar( new Lazy(() => ApplicationContext.Services.ServerRegistrationService), new DatabaseServerRegistrarOptions())); diff --git a/src/Umbraco.Core/DependencyInjection/Current.cs b/src/Umbraco.Core/DependencyInjection/Current.cs index 36dd25737a..67013b5318 100644 --- a/src/Umbraco.Core/DependencyInjection/Current.cs +++ b/src/Umbraco.Core/DependencyInjection/Current.cs @@ -71,6 +71,9 @@ namespace Umbraco.Core.DependencyInjection public static IServerMessenger ServerMessenger => Container.GetInstance(); + public static IServerRegistrar ServerRegistrar + => Container.GetInstance(); + #endregion } } diff --git a/src/Umbraco.Core/Sync/ApplicationUrlHelper.cs b/src/Umbraco.Core/Sync/ApplicationUrlHelper.cs index 7980f2936b..e3a82f41c6 100644 --- a/src/Umbraco.Core/Sync/ApplicationUrlHelper.cs +++ b/src/Umbraco.Core/Sync/ApplicationUrlHelper.cs @@ -2,6 +2,7 @@ using System; using System.Web; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.DependencyInjection; using Umbraco.Core.IO; using Umbraco.Core.ObjectResolution; @@ -114,8 +115,7 @@ namespace Umbraco.Core.Sync // - contain a scheme // - end or not with a slash, it will be taken care of // eg "http://www.mysite.com/umbraco" - var registrar = ServerRegistrarResolver.Current.Registrar; - url = registrar.GetCurrentServerUmbracoApplicationUrl(); + url = Current.ServerRegistrar.GetCurrentServerUmbracoApplicationUrl(); if (url.IsNullOrWhiteSpace() == false) { appContext._umbracoApplicationUrl = url.TrimEnd('/'); diff --git a/src/Umbraco.Core/Sync/ServerRegistrarResolver.cs b/src/Umbraco.Core/Sync/ServerRegistrarResolver.cs deleted file mode 100644 index 3199fad079..0000000000 --- a/src/Umbraco.Core/Sync/ServerRegistrarResolver.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using System.Linq.Expressions; -using LightInject; -using Umbraco.Core.ObjectResolution; - -namespace Umbraco.Core.Sync -{ - /// - /// Resolves the IServerRegistrar object. - /// - public sealed class ServerRegistrarResolver : ContainerSingleObjectResolver - { - /// - /// Initializes a new instance of the class with a registrar. - /// - /// An instance of a registrar. - /// The resolver is created by the CoreBootManager and thus the constructor remains internal. - public ServerRegistrarResolver(IServerRegistrar value) - : base(value) - { } - - internal ServerRegistrarResolver(IServiceContainer container) - : base(container) - { } - - internal ServerRegistrarResolver(IServiceContainer container, Func implementationType) - : base(container, implementationType) - { } - - /// - /// Sets the registrar. - /// - /// The registrar. - /// For developers, at application startup. - public void SetServerRegistrar(IServerRegistrar serverRegistrar) - { - Value = serverRegistrar; - } - - /// - /// Gets the registrar. - /// - public IServerRegistrar Registrar - { - get { return Value; } - } - - } -} \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 2bb844f238..52529b1333 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -1248,7 +1248,6 @@ - diff --git a/src/Umbraco.Tests/ApplicationUrlHelperTests.cs b/src/Umbraco.Tests/ApplicationUrlHelperTests.cs index 608f0cc2cd..6907b68cf9 100644 --- a/src/Umbraco.Tests/ApplicationUrlHelperTests.cs +++ b/src/Umbraco.Tests/ApplicationUrlHelperTests.cs @@ -1,6 +1,7 @@ using System.Configuration; using System.IO; using System.Linq; +using LightInject; using Moq; using NUnit.Framework; using Umbraco.Core; @@ -11,6 +12,7 @@ using Umbraco.Core.ObjectResolution; using Umbraco.Core.Profiling; using Umbraco.Core.Sync; using Umbraco.Tests.TestHelpers; +using Umbraco.Core.DependencyInjection; namespace Umbraco.Tests { @@ -18,6 +20,7 @@ namespace Umbraco.Tests public class ApplicationUrlHelperTests { private ILogger _logger; + private IServerRegistrar _registrar; // note: in tests, read appContext._umbracoApplicationUrl and not the property, // because reading the property does run some code, as long as the field is null. @@ -28,16 +31,18 @@ namespace Umbraco.Tests _logger = new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test-log4net.config"))); } - private static void Initialize(IUmbracoSettingsSection settings) + private void Initialize(IUmbracoSettingsSection settings) { - ServerRegistrarResolver.Current = new ServerRegistrarResolver(new ConfigServerRegistrar(settings.DistributedCall)); - Resolution.Freeze(); + _registrar = new ConfigServerRegistrar(settings.DistributedCall); + var container = new ServiceContainer(); + container.ConfigureUmbracoCore(); + container.Register(_ => _registrar); } [TearDown] public void Reset() { - ServerRegistrarResolver.Reset(); + Current.Reset(); } [Test] @@ -121,8 +126,7 @@ namespace Umbraco.Tests Assert.AreEqual("http://server1.com:80/umbraco", appCtx._umbracoApplicationUrl); - var registrar = ServerRegistrarResolver.Current.Registrar; - var role = registrar.GetCurrentServerRole(); + var role = _registrar.GetCurrentServerRole(); Assert.AreEqual(ServerRole.Master, role); } @@ -152,8 +156,7 @@ namespace Umbraco.Tests Assert.AreEqual("http://server1.com:80/umbraco", appCtx._umbracoApplicationUrl); - var registrar = ServerRegistrarResolver.Current.Registrar; - var role = registrar.GetCurrentServerRole(); + var role = _registrar.GetCurrentServerRole(); Assert.AreEqual(ServerRole.Slave, role); } @@ -183,8 +186,7 @@ namespace Umbraco.Tests Assert.IsNull(appCtx._umbracoApplicationUrl); - var registrar = ServerRegistrarResolver.Current.Registrar; - var role = registrar.GetCurrentServerRole(); + var role = _registrar.GetCurrentServerRole(); Assert.AreEqual(ServerRole.Single, role); } @@ -200,8 +202,7 @@ namespace Umbraco.Tests Initialize(settings); - var registrar = ServerRegistrarResolver.Current.Registrar; - var role = registrar.GetCurrentServerRole(); + var role = _registrar.GetCurrentServerRole(); Assert.AreEqual(ServerRole.Single, role); } @@ -217,8 +218,7 @@ namespace Umbraco.Tests Initialize(settings); - var registrar = ServerRegistrarResolver.Current.Registrar; - var role = registrar.GetCurrentServerRole(); + var role = _registrar.GetCurrentServerRole(); Assert.AreEqual(ServerRole.Unknown, role); } @@ -237,8 +237,7 @@ namespace Umbraco.Tests Initialize(settings); - var registrar = ServerRegistrarResolver.Current.Registrar; - var role = registrar.GetCurrentServerRole(); + var role = _registrar.GetCurrentServerRole(); Assert.AreEqual(ServerRole.Slave, role); } diff --git a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs index c2d0ad9298..13f6351943 100644 --- a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs +++ b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs @@ -25,8 +25,7 @@ namespace Umbraco.Tests.Cache.DistributedCache var container = new ServiceContainer(); container.ConfigureUmbracoCore(); - ServerRegistrarResolver.Current = new ServerRegistrarResolver(new TestServerRegistrar()); - + container.Register(_ => new TestServerRegistrar()); container.Register(_ => new TestServerMessenger(), new PerContainerLifetime()); CacheRefresherCollectionBuilder.Register(container) @@ -38,8 +37,6 @@ namespace Umbraco.Tests.Cache.DistributedCache [TearDown] public void Teardown() { - ServerRegistrarResolver.Reset(); - Current.Reset(); } diff --git a/src/Umbraco.Tests/Integration/ContentEventsTests.cs b/src/Umbraco.Tests/Integration/ContentEventsTests.cs index ca799da8de..12b397e783 100644 --- a/src/Umbraco.Tests/Integration/ContentEventsTests.cs +++ b/src/Umbraco.Tests/Integration/ContentEventsTests.cs @@ -52,8 +52,7 @@ namespace Umbraco.Tests.Integration { base.ConfigureContainer(); - ServerRegistrarResolver.Current = new ServerRegistrarResolver(new DistributedCacheTests.TestServerRegistrar()); // localhost-only - + Container.Register(_ => new DistributedCacheTests.TestServerRegistrar()); // localhost-only Container.Register(new PerContainerLifetime()); CacheRefresherCollectionBuilder.Register(Container) diff --git a/src/Umbraco.Web/Cache/DistributedCache.cs b/src/Umbraco.Web/Cache/DistributedCache.cs index 3e69a897a0..2fb13eae7c 100644 --- a/src/Umbraco.Web/Cache/DistributedCache.cs +++ b/src/Umbraco.Web/Cache/DistributedCache.cs @@ -64,7 +64,7 @@ namespace Umbraco.Web.Cache if (refresherGuid == Guid.Empty || instances.Length == 0 || getNumericId == null) return; Current.ServerMessenger.PerformRefresh( - ServerRegistrarResolver.Current.Registrar.Registrations, + Current.ServerRegistrar.Registrations, GetRefresherById(refresherGuid), getNumericId, instances); @@ -80,7 +80,7 @@ namespace Umbraco.Web.Cache if (refresherGuid == Guid.Empty || id == default(int)) return; Current.ServerMessenger.PerformRefresh( - ServerRegistrarResolver.Current.Registrar.Registrations, + Current.ServerRegistrar.Registrations, GetRefresherById(refresherGuid), id); } @@ -95,7 +95,7 @@ namespace Umbraco.Web.Cache if (refresherGuid == Guid.Empty || id == Guid.Empty) return; Current.ServerMessenger.PerformRefresh( - ServerRegistrarResolver.Current.Registrar.Registrations, + Current.ServerRegistrar.Registrations, GetRefresherById(refresherGuid), id); } @@ -107,7 +107,7 @@ namespace Umbraco.Web.Cache if (refresherGuid == Guid.Empty || payload == null) return; Current.ServerMessenger.PerformRefresh( - ServerRegistrarResolver.Current.Registrar.Registrations, + Current.ServerRegistrar.Registrations, GetRefresherById(refresherGuid), payload); } @@ -119,7 +119,7 @@ namespace Umbraco.Web.Cache if (refresherGuid == Guid.Empty || payloads == null) return; Current.ServerMessenger.PerformRefresh( - ServerRegistrarResolver.Current.Registrar.Registrations, + Current.ServerRegistrar.Registrations, GetRefresherById(refresherGuid), payloads.ToArray()); } @@ -133,7 +133,7 @@ namespace Umbraco.Web.Cache if (refresherGuid == Guid.Empty || jsonPayload.IsNullOrWhiteSpace()) return; Current.ServerMessenger.PerformRefresh( - ServerRegistrarResolver.Current.Registrar.Registrations, + Current.ServerRegistrar.Registrations, GetRefresherById(refresherGuid), jsonPayload); } @@ -148,7 +148,7 @@ namespace Umbraco.Web.Cache // if (refresherId == Guid.Empty || payload == null) return; // Current.ServerMessenger.Notify( - // ServerRegistrarResolver.Current.Registrar.Registrations, + // Current.ServerRegistrar.Registrations, // GetRefresherById(refresherId), // json); //} @@ -162,7 +162,7 @@ namespace Umbraco.Web.Cache if (refresherGuid == Guid.Empty) return; Current.ServerMessenger.PerformRefreshAll( - ServerRegistrarResolver.Current.Registrar.Registrations, + Current.ServerRegistrar.Registrations, GetRefresherById(refresherGuid)); } @@ -176,7 +176,7 @@ namespace Umbraco.Web.Cache if (refresherGuid == Guid.Empty || id == default(int)) return; Current.ServerMessenger.PerformRemove( - ServerRegistrarResolver.Current.Registrar.Registrations, + Current.ServerRegistrar.Registrations, GetRefresherById(refresherGuid), id); } @@ -194,7 +194,7 @@ namespace Umbraco.Web.Cache public void Remove(Guid refresherGuid, Func getNumericId, params T[] instances) { Current.ServerMessenger.PerformRemove( - ServerRegistrarResolver.Current.Registrar.Registrations, + Current.ServerRegistrar.Registrations, GetRefresherById(refresherGuid), getNumericId, instances); diff --git a/src/Umbraco.Web/Current.cs b/src/Umbraco.Web/Current.cs index c3addff7e7..09b0e533ca 100644 --- a/src/Umbraco.Web/Current.cs +++ b/src/Umbraco.Web/Current.cs @@ -225,6 +225,9 @@ namespace Umbraco.Web public static IServerMessenger ServerMessenger => Container.GetInstance(); + public static IServerRegistrar ServerRegistrar + => Container.GetInstance(); + #endregion } } diff --git a/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs b/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs index a6dfa07be1..f2354a71a0 100644 --- a/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs +++ b/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs @@ -34,7 +34,7 @@ namespace Umbraco.Web.Strategies // bind to events protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { - _registrar = ServerRegistrarResolver.Current.Registrar as DatabaseServerRegistrar; + _registrar = Current.ServerRegistrar as DatabaseServerRegistrar; // only for the DatabaseServerRegistrar if (_registrar == null) return;