From 0354fc91bca75b73db5ed601313ea1ce25d3ce8b Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 21 Nov 2019 14:20:27 +0100 Subject: [PATCH] Removed static ApplicationUrlHelper and introduced IBackOfficeInfo and AspNetBackOfficeInfo --- src/Umbraco.Core/RuntimeState.cs | 54 ++++++++++++------- .../Routing/UmbracoModuleTests.cs | 2 +- src/Umbraco.Tests/Testing/UmbracoTestBase.cs | 4 ++ 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/Umbraco.Core/RuntimeState.cs b/src/Umbraco.Core/RuntimeState.cs index 868473965a..4af791b857 100644 --- a/src/Umbraco.Core/RuntimeState.cs +++ b/src/Umbraco.Core/RuntimeState.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Threading; -using System.Web; using Semver; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; @@ -28,13 +27,15 @@ namespace Umbraco.Core private readonly Lazy _serverRegistrar; private readonly IUmbracoVersion _umbracoVersion; private readonly IHostingEnvironment _hostingEnvironment; + private readonly IBackOfficeInfo _backOfficeInfo; /// /// Initializes a new instance of the class. /// public RuntimeState(ILogger logger, IUmbracoSettingsSection settings, IGlobalSettings globalSettings, Lazy mainDom, Lazy serverRegistrar, IUmbracoVersion umbracoVersion, - IHostingEnvironment hostingEnvironment) + IHostingEnvironment hostingEnvironment, + IBackOfficeInfo backOfficeInfo) { _logger = logger; _settings = settings; @@ -43,6 +44,7 @@ namespace Umbraco.Core _serverRegistrar = serverRegistrar; _umbracoVersion = umbracoVersion; _hostingEnvironment = hostingEnvironment; + _backOfficeInfo = backOfficeInfo; ApplicationVirtualPath = _hostingEnvironment.ApplicationVirtualPath; } @@ -99,31 +101,45 @@ namespace Umbraco.Core /// public RuntimeLevelReason Reason { get; internal set; } = RuntimeLevelReason.Unknown; - /// - /// Ensures that the property has a value. - /// - /// - internal void EnsureApplicationUrl(HttpRequestBase request = null) + + internal void EnsureApplicationUrl() { - //Fixme: This causes problems with site swap on azure because azure pre-warms a site by calling into `localhost` and when it does that - // it changes the URL to `localhost:80` which actually doesn't work for pinging itself, it only works internally in Azure. The ironic part - // about this is that this is here specifically for the slot swap scenario https://issues.umbraco.org/issue/U4-10626 + var url = _backOfficeInfo.GetAbsoluteUrl; - - // see U4-10626 - in some cases we want to reset the application url - // (this is a simplified version of what was in 7.x) - // note: should this be optional? is it expensive? - var url = request == null ? null : ApplicationUrlHelper.GetApplicationUrlFromCurrentRequest(request, _globalSettings); var change = url != null && !_applicationUrls.Contains(url); + if (change) { - _logger.Info(typeof(ApplicationUrlHelper), "New url {Url} detected, re-discovering application url.", url); + _logger.Info("New url {Url} detected, re-discovering application url.", url); _applicationUrls.Add(url); + ApplicationUrl = new Uri(url); } - - if (ApplicationUrl != null && !change) return; - ApplicationUrl = new Uri(ApplicationUrlHelper.GetApplicationUrl(_logger, _globalSettings, _settings, ServerRegistrar, request)); } +// /// +// /// Ensures that the property has a value. +// /// +// /// +// internal void EnsureApplicationUrl(HttpRequestBase request = null) +// { +// //Fixme: This causes problems with site swap on azure because azure pre-warms a site by calling into `localhost` and when it does that +// // it changes the URL to `localhost:80` which actually doesn't work for pinging itself, it only works internally in Azure. The ironic part +// // about this is that this is here specifically for the slot swap scenario https://issues.umbraco.org/issue/U4-10626 +// +// +// // see U4-10626 - in some cases we want to reset the application url +// // (this is a simplified version of what was in 7.x) +// // note: should this be optional? is it expensive? +// var url = request == null ? null : ApplicationUrlHelper.GetApplicationUrlFromCurrentRequest(request, _globalSettings); +// var change = url != null && !_applicationUrls.Contains(url); +// if (change) +// { +// _logger.Info(typeof(ApplicationUrlHelper), "New url {Url} detected, re-discovering application url.", url); +// _applicationUrls.Add(url); +// } +// +// if (ApplicationUrl != null && !change) return; +// ApplicationUrl = new Uri(ApplicationUrlHelper.GetApplicationUrl(_logger, _globalSettings, _settings, ServerRegistrar, request)); +// } /// public BootFailedException BootFailedException { get; internal set; } diff --git a/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs b/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs index 59e1c0e4f8..bfb3bc6ac0 100644 --- a/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs +++ b/src/Umbraco.Tests/Routing/UmbracoModuleTests.cs @@ -33,7 +33,7 @@ namespace Umbraco.Tests.Routing var logger = Mock.Of(); var globalSettings = TestObjects.GetGlobalSettings(); var runtime = new RuntimeState(logger, Mock.Of(), globalSettings, - new Lazy(), new Lazy(), UmbracoVersion, HostingEnvironment); + new Lazy(), new Lazy(), UmbracoVersion, HostingEnvironment, BackOfficeInfo); _module = new UmbracoInjectedModule ( diff --git a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs index 283f515ba1..e634d600ea 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs @@ -111,6 +111,7 @@ namespace Umbraco.Tests.Testing protected virtual IProfilingLogger ProfilingLogger => Factory.GetInstance(); protected IHostingEnvironment HostingEnvironment => Factory.GetInstance(); + protected IBackOfficeInfo BackOfficeInfo => Factory.GetInstance(); protected AppCaches AppCaches => Factory.GetInstance(); protected virtual ISqlSyntaxProvider SqlSyntax => Factory.GetInstance(); @@ -143,7 +144,9 @@ namespace Umbraco.Tests.Testing TypeFinder = new TypeFinder(logger); var appCaches = GetAppCaches(); var globalSettings = SettingsForTests.GetDefaultGlobalSettings(); + var settings = SettingsForTests.GetDefaultUmbracoSettings(); IHostingEnvironment hostingEnvironment = new AspNetHostingEnvironment(globalSettings, IOHelper); + IBackOfficeInfo backOfficeInfo = new AspNetBackOfficeInfo(globalSettings, IOHelper, settings, logger); UmbracoVersion = new UmbracoVersion(globalSettings); var typeLoader = GetTypeLoader(IOHelper, TypeFinder, appCaches.RuntimeCache, hostingEnvironment, proflogger, Options.TypeLoader); @@ -161,6 +164,7 @@ namespace Umbraco.Tests.Testing Composition.RegisterUnique(proflogger); Composition.RegisterUnique(appCaches); Composition.RegisterUnique(hostingEnvironment); + Composition.RegisterUnique(backOfficeInfo); TestObjects = new TestObjects(register); Compose();