From b87630250b5f86de83732c6c982b063145c78a43 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Thu, 3 Sep 2020 11:36:57 +0200 Subject: [PATCH] Removed unused private field and constructor variable for configuration global settings from UmbracoVersion. Updated singleton created instances in CreateCompositionRoot to accept IOptions based parameters for configuration. --- .../Composing/TypeFinderConfig.cs | 5 ++-- .../Configuration/UmbracoVersion.cs | 9 ------- src/Umbraco.Tests.Common/TestHelperBase.cs | 2 +- .../Implementations/TestHelper.cs | 3 ++- .../Extensions/UriExtensionsTests.cs | 5 ++-- src/Umbraco.Tests/Testing/UmbracoTestBase.cs | 2 +- .../AspNetCore/AspNetCoreBackOfficeInfo.cs | 7 +---- .../AspNetCoreHostingEnvironment.cs | 9 ++----- .../UmbracoCoreServiceCollectionExtensions.cs | 27 +++++++++++-------- src/Umbraco.Web/UmbracoApplicationBase.cs | 2 +- 10 files changed, 30 insertions(+), 41 deletions(-) diff --git a/src/Umbraco.Core/Composing/TypeFinderConfig.cs b/src/Umbraco.Core/Composing/TypeFinderConfig.cs index cf043ab381..ef862fd49d 100644 --- a/src/Umbraco.Core/Composing/TypeFinderConfig.cs +++ b/src/Umbraco.Core/Composing/TypeFinderConfig.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Microsoft.Extensions.Options; using Umbraco.Core.Configuration.Models; using Umbraco.Core.Configuration.UmbracoSettings; @@ -14,9 +15,9 @@ namespace Umbraco.Core.Composing private readonly TypeFinderSettings _settings; private IEnumerable _assembliesAcceptingLoadExceptions; - public TypeFinderConfig(TypeFinderSettings settings) + public TypeFinderConfig(IOptions settings) { - _settings = settings; + _settings = settings.Value; } public IEnumerable AssembliesAcceptingLoadExceptions diff --git a/src/Umbraco.Core/Configuration/UmbracoVersion.cs b/src/Umbraco.Core/Configuration/UmbracoVersion.cs index 3ae59189fa..2e45be6cc6 100644 --- a/src/Umbraco.Core/Configuration/UmbracoVersion.cs +++ b/src/Umbraco.Core/Configuration/UmbracoVersion.cs @@ -1,7 +1,6 @@ using System; using System.Reflection; using Semver; -using Umbraco.Core.Configuration.Models; namespace Umbraco.Core.Configuration { @@ -10,14 +9,6 @@ namespace Umbraco.Core.Configuration /// public class UmbracoVersion : IUmbracoVersion { - private readonly GlobalSettings _globalSettings; - - public UmbracoVersion(GlobalSettings globalSettings) - : this() - { - _globalSettings = globalSettings; - } - public UmbracoVersion() { var umbracoCoreAssembly = typeof(SemVersion).Assembly; diff --git a/src/Umbraco.Tests.Common/TestHelperBase.cs b/src/Umbraco.Tests.Common/TestHelperBase.cs index 3e50cf2a53..df5f67a206 100644 --- a/src/Umbraco.Tests.Common/TestHelperBase.cs +++ b/src/Umbraco.Tests.Common/TestHelperBase.cs @@ -128,7 +128,7 @@ namespace Umbraco.Tests.Common return relativePath.Replace("~/", bin + "/"); } - public IUmbracoVersion GetUmbracoVersion() => new UmbracoVersion(new GlobalSettingsBuilder().Build()); + public IUmbracoVersion GetUmbracoVersion() => new UmbracoVersion(); public IRegister GetRegister() { diff --git a/src/Umbraco.Tests.Integration/Implementations/TestHelper.cs b/src/Umbraco.Tests.Integration/Implementations/TestHelper.cs index affb274662..c2beb3cdb3 100644 --- a/src/Umbraco.Tests.Integration/Implementations/TestHelper.cs +++ b/src/Umbraco.Tests.Integration/Implementations/TestHelper.cs @@ -20,6 +20,7 @@ using Umbraco.Tests.Common; using Umbraco.Web.Common.AspNetCore; using IHostingEnvironment = Umbraco.Core.Hosting.IHostingEnvironment; using Umbraco.Tests.Common.Builders; +using Microsoft.Extensions.Options; namespace Umbraco.Tests.Integration.Implementations { @@ -105,7 +106,7 @@ namespace Umbraco.Tests.Integration.Implementations if (_backOfficeInfo == null) { var globalSettings = new GlobalSettingsBuilder().Build(); - _backOfficeInfo = new AspNetCoreBackOfficeInfo(globalSettings); + _backOfficeInfo = new AspNetCoreBackOfficeInfo(Options.Create(globalSettings)); } return _backOfficeInfo; diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Core/Extensions/UriExtensionsTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Core/Extensions/UriExtensionsTests.cs index 9a7b7f63ed..5e394eeffb 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Core/Extensions/UriExtensionsTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Core/Extensions/UriExtensionsTests.cs @@ -1,5 +1,6 @@ using System; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Options; using Moq; using NUnit.Framework; using Umbraco.Core; @@ -48,7 +49,7 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Extensions public void Is_Back_Office_Request(string input, string virtualPath, bool expected) { var hostingSettings = new HostingSettingsBuilder().WithApplicationVirtualPath(virtualPath).Build(); - var hostingEnvironment = new AspNetCoreHostingEnvironment(hostingSettings, _hostEnvironment); + var hostingEnvironment = new AspNetCoreHostingEnvironment(Options.Create(hostingSettings), _hostEnvironment); var source = new Uri(input); Assert.AreEqual(expected, source.IsBackOfficeRequest(_globalSettings, hostingEnvironment)); @@ -67,7 +68,7 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Extensions { var source = new Uri(input); var hostingSettings = new HostingSettingsBuilder().Build(); - var hostingEnvironment = new AspNetCoreHostingEnvironment(hostingSettings, _hostEnvironment); + var hostingEnvironment = new AspNetCoreHostingEnvironment(Options.Create(hostingSettings), _hostEnvironment); Assert.AreEqual(expected, source.IsInstallerRequest(hostingEnvironment)); } diff --git a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs index b8cab0d0f7..affce75c20 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs @@ -178,7 +178,7 @@ namespace Umbraco.Tests.Testing IBackOfficeInfo backOfficeInfo = new AspNetBackOfficeInfo(ConfigModelConversionsToLegacy.ConvertGlobalSettings(globalSettings), IOHelper, logger, settings); IIpResolver ipResolver = new AspNetIpResolver(); - UmbracoVersion = new UmbracoVersion(globalSettings); + UmbracoVersion = new UmbracoVersion(); LocalizedTextService = new LocalizedTextService(new Dictionary>(), logger); diff --git a/src/Umbraco.Web.Common/AspNetCore/AspNetCoreBackOfficeInfo.cs b/src/Umbraco.Web.Common/AspNetCore/AspNetCoreBackOfficeInfo.cs index 6f40800307..edc5efdd1e 100644 --- a/src/Umbraco.Web.Common/AspNetCore/AspNetCoreBackOfficeInfo.cs +++ b/src/Umbraco.Web.Common/AspNetCore/AspNetCoreBackOfficeInfo.cs @@ -7,13 +7,8 @@ namespace Umbraco.Web.Common.AspNetCore public class AspNetCoreBackOfficeInfo : IBackOfficeInfo { public AspNetCoreBackOfficeInfo(IOptions globalSettings) - : this(globalSettings.Value) { - } - - public AspNetCoreBackOfficeInfo(GlobalSettings globalSettings) - { - GetAbsoluteUrl = globalSettings.UmbracoPath; + GetAbsoluteUrl = globalSettings.Value.UmbracoPath; } public string GetAbsoluteUrl { get; } // TODO make absolute diff --git a/src/Umbraco.Web.Common/AspNetCore/AspNetCoreHostingEnvironment.cs b/src/Umbraco.Web.Common/AspNetCore/AspNetCoreHostingEnvironment.cs index 32c291bbce..84518c16de 100644 --- a/src/Umbraco.Web.Common/AspNetCore/AspNetCoreHostingEnvironment.cs +++ b/src/Umbraco.Web.Common/AspNetCore/AspNetCoreHostingEnvironment.cs @@ -16,13 +16,8 @@ namespace Umbraco.Web.Common.AspNetCore private string _localTempPath; public AspNetCoreHostingEnvironment(IOptions hostingSettings, IWebHostEnvironment webHostEnvironment) - : this(hostingSettings.Value, webHostEnvironment) { - } - - public AspNetCoreHostingEnvironment(HostingSettings hostingSettings, IWebHostEnvironment webHostEnvironment) - { - _hostingSettings = hostingSettings ?? throw new ArgumentNullException(nameof(hostingSettings)); + _hostingSettings = hostingSettings.Value ?? throw new ArgumentNullException(nameof(hostingSettings)); _webHostEnvironment = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment)); SiteName = webHostEnvironment.ApplicationName; @@ -30,7 +25,7 @@ namespace Umbraco.Web.Common.AspNetCore ApplicationPhysicalPath = webHostEnvironment.ContentRootPath; //TODO how to find this, This is a server thing, not application thing. - ApplicationVirtualPath = hostingSettings.ApplicationVirtualPath?.EnsureStartsWith('/') + ApplicationVirtualPath = _hostingSettings.ApplicationVirtualPath?.EnsureStartsWith('/') ?? "/"; IISVersion = new Version(0, 0); // TODO not necessary IIS diff --git a/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs b/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs index b514b4d918..fb16d0db8e 100644 --- a/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs @@ -126,6 +126,12 @@ namespace Umbraco.Extensions services.Configure(configuration.GetSection(Constants.Configuration.ConfigSecurityPrefix + "UserPassword")); services.Configure(configuration.GetSection(Constants.Configuration.ConfigPrefix + "WebRouting")); + //services.Configure(configuration.GetSection(Constants.Configuration.ConfigPrefix + "Tours")); + services.Configure(settings => + { + settings.EnableTours = false; + }); + // TODO: remove this once no longer requred in Umbraco.Web. var configsFactory = new AspNetCoreConfigsFactory(configuration); var configs = configsFactory.Create(); @@ -224,10 +230,10 @@ namespace Umbraco.Extensions // `RegisterEssentials`. var serviceProvider = services.BuildServiceProvider(); - var globalSettings = serviceProvider.GetService>().Value; - var connectionStrings = serviceProvider.GetService>().Value; - var hostingSettings = serviceProvider.GetService>().Value; - var typeFinderSettings = serviceProvider.GetService>().Value; + var globalSettings = serviceProvider.GetService>(); + var connectionStrings = serviceProvider.GetService>(); + var hostingSettings = serviceProvider.GetService>(); + var typeFinderSettings = serviceProvider.GetService>(); var dbProviderFactoryCreator = serviceProvider.GetRequiredService(); @@ -238,14 +244,14 @@ namespace Umbraco.Extensions loggingConfiguration, out var logger, out var ioHelper, out var hostingEnvironment, out var backOfficeInfo, out var profiler); - var umbracoVersion = new UmbracoVersion(globalSettings); + var umbracoVersion = new UmbracoVersion(); var typeFinder = CreateTypeFinder(logger, profiler, webHostEnvironment, entryAssembly, typeFinderSettings); var configs = serviceProvider.GetService(); var coreRuntime = GetCoreRuntime( configs, - globalSettings, - connectionStrings, + globalSettings.Value, + connectionStrings.Value, umbracoVersion, ioHelper, logger, @@ -261,7 +267,7 @@ namespace Umbraco.Extensions return services; } - private static ITypeFinder CreateTypeFinder(Core.Logging.ILogger logger, IProfiler profiler, IWebHostEnvironment webHostEnvironment, Assembly entryAssembly, TypeFinderSettings typeFinderSettings) + private static ITypeFinder CreateTypeFinder(Core.Logging.ILogger logger, IProfiler profiler, IWebHostEnvironment webHostEnvironment, Assembly entryAssembly, IOptions typeFinderSettings) { var runtimeHashPaths = new RuntimeHashPaths(); runtimeHashPaths.AddFolder(new DirectoryInfo(Path.Combine(webHostEnvironment.ContentRootPath, "bin"))); @@ -305,8 +311,8 @@ namespace Umbraco.Extensions private static IServiceCollection CreateCompositionRoot( IServiceCollection services, - GlobalSettings globalSettings, - HostingSettings hostingSettings, + IOptions globalSettings, + IOptions hostingSettings, IWebHostEnvironment webHostEnvironment, ILoggingConfiguration loggingConfiguration, out Core.Logging.ILogger logger, @@ -318,7 +324,6 @@ namespace Umbraco.Extensions if (globalSettings == null) throw new InvalidOperationException($"Could not resolve type {typeof(GlobalSettings)} from the container, ensure {nameof(AddUmbracoConfiguration)} is called before calling {nameof(AddUmbracoCore)}"); - hostingEnvironment = new AspNetCoreHostingEnvironment(hostingSettings, webHostEnvironment); ioHelper = new IOHelper(hostingEnvironment); logger = AddLogger(services, hostingEnvironment, loggingConfiguration); diff --git a/src/Umbraco.Web/UmbracoApplicationBase.cs b/src/Umbraco.Web/UmbracoApplicationBase.cs index 4f6f2c7f0f..8599464bd1 100644 --- a/src/Umbraco.Web/UmbracoApplicationBase.cs +++ b/src/Umbraco.Web/UmbracoApplicationBase.cs @@ -160,7 +160,7 @@ namespace Umbraco.Web var globalSettings = Umbraco.Composing.Current.Configs.Global(); - var umbracoVersion = new UmbracoVersion(ConfigModelConversionsFromLegacy.ConvertGlobalSettings(globalSettings)); + var umbracoVersion = new UmbracoVersion(); // create the register for the application, and boot // the boot manager is responsible for registrations