From 10c2561d0a8836d6bde6ca0ab09e77e99d618143 Mon Sep 17 00:00:00 2001 From: Nikolaj Geisle Date: Thu, 23 Sep 2021 12:54:12 +0200 Subject: [PATCH] Updated Classes to use IOptionsMonitor or IOptionsSnapshot instead of IOptions --- .../NotificationMethods/EmailNotificationMethod.cs | 6 +++--- .../NotificationMethods/NotificationMethodBase.cs | 4 ++-- src/Umbraco.Core/Models/ContentEditing/UserInvite.cs | 2 +- src/Umbraco.Core/Packaging/PackagesRepository.cs | 4 ++-- .../PublishedCache/DefaultCultureAccessor.cs | 9 ++++----- src/Umbraco.Core/Routing/ContentFinderByIdPath.cs | 8 +++++--- .../Routing/ContentFinderByUrlAndTemplate.cs | 7 ++++--- src/Umbraco.Core/Routing/DefaultUrlProvider.cs | 7 ++++--- src/Umbraco.Core/Routing/PublishedRouter.cs | 9 +++++---- src/Umbraco.Core/Routing/UrlProvider.cs | 6 +++--- src/Umbraco.Core/Runtime/EssentialDirectoryCreator.cs | 4 ++-- .../DependencyInjection/UmbracoBuilder.Services.cs | 2 +- src/Umbraco.Tests.Common/TestOptionsSnapshot.cs | 11 +++++++++++ .../Packaging/CreatedPackagesRepositoryTests.cs | 5 ++--- .../Templates/HtmlImageSourceParserTests.cs | 2 +- .../Templates/HtmlLocalLinkParserTests.cs | 2 +- 16 files changed, 51 insertions(+), 37 deletions(-) create mode 100644 src/Umbraco.Tests.Common/TestOptionsSnapshot.cs diff --git a/src/Umbraco.Core/HealthChecks/NotificationMethods/EmailNotificationMethod.cs b/src/Umbraco.Core/HealthChecks/NotificationMethods/EmailNotificationMethod.cs index 8de4cd8cc3..fcd71da5bd 100644 --- a/src/Umbraco.Core/HealthChecks/NotificationMethods/EmailNotificationMethod.cs +++ b/src/Umbraco.Core/HealthChecks/NotificationMethods/EmailNotificationMethod.cs @@ -24,8 +24,8 @@ namespace Umbraco.Cms.Core.HealthChecks.NotificationMethods ILocalizedTextService textService, IHostingEnvironment hostingEnvironment, IEmailSender emailSender, - IOptions healthChecksSettings, - IOptions contentSettings, + IOptionsMonitor healthChecksSettings, + IOptionsMonitor contentSettings, IMarkdownToHtmlConverter markdownToHtmlConverter) : base(healthChecksSettings) { @@ -42,7 +42,7 @@ namespace Umbraco.Cms.Core.HealthChecks.NotificationMethods _hostingEnvironment = hostingEnvironment; _emailSender = emailSender; _markdownToHtmlConverter = markdownToHtmlConverter; - _contentSettings = contentSettings.Value ?? throw new ArgumentNullException(nameof(contentSettings)); + _contentSettings = contentSettings.CurrentValue ?? throw new ArgumentNullException(nameof(contentSettings)); } public string RecipientEmail { get; } diff --git a/src/Umbraco.Core/HealthChecks/NotificationMethods/NotificationMethodBase.cs b/src/Umbraco.Core/HealthChecks/NotificationMethods/NotificationMethodBase.cs index 10af1de106..3a889154d2 100644 --- a/src/Umbraco.Core/HealthChecks/NotificationMethods/NotificationMethodBase.cs +++ b/src/Umbraco.Core/HealthChecks/NotificationMethods/NotificationMethodBase.cs @@ -8,7 +8,7 @@ namespace Umbraco.Cms.Core.HealthChecks.NotificationMethods { public abstract class NotificationMethodBase : IHealthCheckNotificationMethod { - protected NotificationMethodBase(IOptions healthCheckSettings) + protected NotificationMethodBase(IOptionsMonitor healthCheckSettings) { var type = GetType(); var attribute = type.GetCustomAttribute(); @@ -18,7 +18,7 @@ namespace Umbraco.Cms.Core.HealthChecks.NotificationMethods return; } - var notificationMethods = healthCheckSettings.Value.Notification.NotificationMethods; + var notificationMethods = healthCheckSettings.CurrentValue.Notification.NotificationMethods; if (!notificationMethods.TryGetValue(attribute.Alias, out var notificationMethod)) { Enabled = false; diff --git a/src/Umbraco.Core/Models/ContentEditing/UserInvite.cs b/src/Umbraco.Core/Models/ContentEditing/UserInvite.cs index 6eb5c12ddf..6db43e99ae 100644 --- a/src/Umbraco.Core/Models/ContentEditing/UserInvite.cs +++ b/src/Umbraco.Core/Models/ContentEditing/UserInvite.cs @@ -35,7 +35,7 @@ namespace Umbraco.Cms.Core.Models.ContentEditing if (UserGroups.Any() == false) yield return new ValidationResult("A user must be assigned to at least one group", new[] { nameof(UserGroups) }); - var securitySettings = validationContext.GetRequiredService>(); + var securitySettings = validationContext.GetRequiredService>(); if (securitySettings.Value.UsernameIsEmail == false && Username.IsNullOrWhiteSpace()) yield return new ValidationResult("A username cannot be empty", new[] { nameof(Username) }); diff --git a/src/Umbraco.Core/Packaging/PackagesRepository.cs b/src/Umbraco.Core/Packaging/PackagesRepository.cs index 2ab24fa593..76d8b75ba8 100644 --- a/src/Umbraco.Core/Packaging/PackagesRepository.cs +++ b/src/Umbraco.Core/Packaging/PackagesRepository.cs @@ -68,7 +68,7 @@ namespace Umbraco.Cms.Core.Packaging ILocalizationService languageService, IHostingEnvironment hostingEnvironment, IEntityXmlSerializer serializer, - IOptions globalSettings, + IOptionsMonitor globalSettings, IMediaService mediaService, IMediaTypeService mediaTypeService, MediaFileManager mediaFileManager, @@ -92,7 +92,7 @@ namespace Umbraco.Cms.Core.Packaging _tempFolderPath = tempFolderPath ?? Constants.SystemDirectories.TempData.EnsureEndsWith('/') + "PackageFiles"; _packagesFolderPath = packagesFolderPath ?? Constants.SystemDirectories.Packages; - _mediaFolderPath = mediaFolderPath ?? globalSettings.Value.UmbracoMediaPath + "/created-packages"; + _mediaFolderPath = mediaFolderPath ?? globalSettings.CurrentValue.UmbracoMediaPath + "/created-packages"; _parser = new PackageDefinitionXmlParser(); _mediaService = mediaService; diff --git a/src/Umbraco.Core/PublishedCache/DefaultCultureAccessor.cs b/src/Umbraco.Core/PublishedCache/DefaultCultureAccessor.cs index 145206daf1..8d40489ae4 100644 --- a/src/Umbraco.Core/PublishedCache/DefaultCultureAccessor.cs +++ b/src/Umbraco.Core/PublishedCache/DefaultCultureAccessor.cs @@ -11,23 +11,22 @@ namespace Umbraco.Cms.Core.PublishedCache { private readonly ILocalizationService _localizationService; private readonly IRuntimeState _runtimeState; - private readonly IOptions _options; + private readonly GlobalSettings _options; /// /// Initializes a new instance of the class. /// - public DefaultCultureAccessor(ILocalizationService localizationService, IRuntimeState runtimeState, IOptions options) + public DefaultCultureAccessor(ILocalizationService localizationService, IRuntimeState runtimeState, IOptionsSnapshot options) { _localizationService = localizationService; _runtimeState = runtimeState; - _options = options; - + _options = options.Value; } /// public string DefaultCulture => _runtimeState.Level == RuntimeLevel.Run ? _localizationService.GetDefaultLanguageIsoCode() ?? "" // fast - : _options.Value.DefaultUILanguage; // default for install and upgrade, when the service is n/a + : _options.DefaultUILanguage; // default for install and upgrade, when the service is n/a } } diff --git a/src/Umbraco.Core/Routing/ContentFinderByIdPath.cs b/src/Umbraco.Core/Routing/ContentFinderByIdPath.cs index d8093aed1b..1ff54c1546 100644 --- a/src/Umbraco.Core/Routing/ContentFinderByIdPath.cs +++ b/src/Umbraco.Core/Routing/ContentFinderByIdPath.cs @@ -18,21 +18,23 @@ namespace Umbraco.Cms.Core.Routing private readonly ILogger _logger; private readonly IRequestAccessor _requestAccessor; private readonly IUmbracoContextAccessor _umbracoContextAccessor; - private readonly WebRoutingSettings _webRoutingSettings; + private WebRoutingSettings _webRoutingSettings; /// /// Initializes a new instance of the class. /// public ContentFinderByIdPath( - IOptions webRoutingSettings, + IOptionsMonitor webRoutingSettings, ILogger logger, IRequestAccessor requestAccessor, IUmbracoContextAccessor umbracoContextAccessor) { - _webRoutingSettings = webRoutingSettings.Value ?? throw new System.ArgumentNullException(nameof(webRoutingSettings)); + _webRoutingSettings = webRoutingSettings.CurrentValue ?? throw new System.ArgumentNullException(nameof(webRoutingSettings)); _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); _requestAccessor = requestAccessor ?? throw new System.ArgumentNullException(nameof(requestAccessor)); _umbracoContextAccessor = umbracoContextAccessor ?? throw new System.ArgumentNullException(nameof(umbracoContextAccessor)); + + webRoutingSettings.OnChange(x => _webRoutingSettings = x); } /// diff --git a/src/Umbraco.Core/Routing/ContentFinderByUrlAndTemplate.cs b/src/Umbraco.Core/Routing/ContentFinderByUrlAndTemplate.cs index 2e5515fef2..f8eb328eed 100644 --- a/src/Umbraco.Core/Routing/ContentFinderByUrlAndTemplate.cs +++ b/src/Umbraco.Core/Routing/ContentFinderByUrlAndTemplate.cs @@ -23,7 +23,7 @@ namespace Umbraco.Cms.Core.Routing private readonly IFileService _fileService; private readonly IContentTypeService _contentTypeService; - private readonly WebRoutingSettings _webRoutingSettings; + private WebRoutingSettings _webRoutingSettings; /// /// Initializes a new instance of the class. @@ -33,13 +33,14 @@ namespace Umbraco.Cms.Core.Routing IFileService fileService, IContentTypeService contentTypeService, IUmbracoContextAccessor umbracoContextAccessor, - IOptions webRoutingSettings) + IOptionsMonitor webRoutingSettings) : base(logger, umbracoContextAccessor) { _logger = logger; _fileService = fileService; _contentTypeService = contentTypeService; - _webRoutingSettings = webRoutingSettings.Value; + _webRoutingSettings = webRoutingSettings.CurrentValue; + webRoutingSettings.OnChange(x => _webRoutingSettings = x); } /// diff --git a/src/Umbraco.Core/Routing/DefaultUrlProvider.cs b/src/Umbraco.Core/Routing/DefaultUrlProvider.cs index 16248854e5..969e5bf98b 100644 --- a/src/Umbraco.Core/Routing/DefaultUrlProvider.cs +++ b/src/Umbraco.Core/Routing/DefaultUrlProvider.cs @@ -15,19 +15,20 @@ namespace Umbraco.Cms.Core.Routing /// public class DefaultUrlProvider : IUrlProvider { - private readonly RequestHandlerSettings _requestSettings; + private RequestHandlerSettings _requestSettings; private readonly ILogger _logger; private readonly ISiteDomainMapper _siteDomainMapper; private readonly IUmbracoContextAccessor _umbracoContextAccessor; private readonly UriUtility _uriUtility; - public DefaultUrlProvider(IOptions requestSettings, ILogger logger, ISiteDomainMapper siteDomainMapper, IUmbracoContextAccessor umbracoContextAccessor, UriUtility uriUtility) + public DefaultUrlProvider(IOptionsMonitor requestSettings, ILogger logger, ISiteDomainMapper siteDomainMapper, IUmbracoContextAccessor umbracoContextAccessor, UriUtility uriUtility) { - _requestSettings = requestSettings.Value; + _requestSettings = requestSettings.CurrentValue; _logger = logger; _siteDomainMapper = siteDomainMapper; _uriUtility = uriUtility; _umbracoContextAccessor = umbracoContextAccessor; + requestSettings.OnChange(x => _requestSettings = x); } #region GetUrl diff --git a/src/Umbraco.Core/Routing/PublishedRouter.cs b/src/Umbraco.Core/Routing/PublishedRouter.cs index 548387a675..f069adffeb 100644 --- a/src/Umbraco.Core/Routing/PublishedRouter.cs +++ b/src/Umbraco.Core/Routing/PublishedRouter.cs @@ -24,7 +24,7 @@ namespace Umbraco.Cms.Core.Routing /// public class PublishedRouter : IPublishedRouter { - private readonly WebRoutingSettings _webRoutingSettings; + private WebRoutingSettings _webRoutingSettings; private readonly ContentFinderCollection _contentFinders; private readonly IContentLastChanceFinder _contentLastChanceFinder; private readonly IProfilingLogger _profilingLogger; @@ -42,7 +42,7 @@ namespace Umbraco.Cms.Core.Routing /// Initializes a new instance of the class. /// public PublishedRouter( - IOptions webRoutingSettings, + IOptionsMonitor webRoutingSettings, ContentFinderCollection contentFinders, IContentLastChanceFinder contentLastChanceFinder, IVariationContextAccessor variationContextAccessor, @@ -56,7 +56,7 @@ namespace Umbraco.Cms.Core.Routing IUmbracoContextAccessor umbracoContextAccessor, IEventAggregator eventAggregator) { - _webRoutingSettings = webRoutingSettings.Value ?? throw new ArgumentNullException(nameof(webRoutingSettings)); + _webRoutingSettings = webRoutingSettings.CurrentValue ?? throw new ArgumentNullException(nameof(webRoutingSettings)); _contentFinders = contentFinders ?? throw new ArgumentNullException(nameof(contentFinders)); _contentLastChanceFinder = contentLastChanceFinder ?? throw new ArgumentNullException(nameof(contentLastChanceFinder)); _profilingLogger = proflog ?? throw new ArgumentNullException(nameof(proflog)); @@ -69,6 +69,7 @@ namespace Umbraco.Cms.Core.Routing _contentTypeService = contentTypeService; _umbracoContextAccessor = umbracoContextAccessor; _eventAggregator = eventAggregator; + webRoutingSettings.OnChange(x => _webRoutingSettings = x); } /// @@ -237,7 +238,7 @@ namespace Umbraco.Cms.Core.Routing // re-route await RouteRequestInternalAsync(builder); - + // return if we are redirect if (builder.IsRedirect()) { diff --git a/src/Umbraco.Core/Routing/UrlProvider.cs b/src/Umbraco.Core/Routing/UrlProvider.cs index 018343d02a..cf4f4b21f0 100644 --- a/src/Umbraco.Core/Routing/UrlProvider.cs +++ b/src/Umbraco.Core/Routing/UrlProvider.cs @@ -25,7 +25,7 @@ namespace Umbraco.Cms.Core.Routing /// The list of media URL providers. /// The current variation accessor. /// - public UrlProvider(IUmbracoContextAccessor umbracoContextAccessor, IOptions routingSettings, UrlProviderCollection urlProviders, MediaUrlProviderCollection mediaUrlProviders, IVariationContextAccessor variationContextAccessor) + public UrlProvider(IUmbracoContextAccessor umbracoContextAccessor, IOptionsSnapshot routingSettings, UrlProviderCollection urlProviders, MediaUrlProviderCollection mediaUrlProviders, IVariationContextAccessor variationContextAccessor) { _umbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor)); _urlProviders = urlProviders; @@ -122,7 +122,7 @@ namespace Umbraco.Cms.Core.Routing var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); current = umbracoContext.CleanedUmbracoUrl; } - + var url = _urlProviders.Select(provider => provider.GetUrl(content, mode, culture, current)) .FirstOrDefault(u => u != null); @@ -230,7 +230,7 @@ namespace Umbraco.Cms.Core.Routing var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); current = umbracoContext.CleanedUmbracoUrl; } - + var url = _mediaUrlProviders.Select(provider => provider.GetMediaUrl(content, propertyAlias, mode, culture, current)) diff --git a/src/Umbraco.Core/Runtime/EssentialDirectoryCreator.cs b/src/Umbraco.Core/Runtime/EssentialDirectoryCreator.cs index a9564712c3..42d041211d 100644 --- a/src/Umbraco.Core/Runtime/EssentialDirectoryCreator.cs +++ b/src/Umbraco.Core/Runtime/EssentialDirectoryCreator.cs @@ -13,11 +13,11 @@ namespace Umbraco.Cms.Core.Runtime private readonly IHostingEnvironment _hostingEnvironment; private readonly GlobalSettings _globalSettings; - public EssentialDirectoryCreator(IIOHelper ioHelper, IHostingEnvironment hostingEnvironment, IOptions globalSettings) + public EssentialDirectoryCreator(IIOHelper ioHelper, IHostingEnvironment hostingEnvironment, IOptionsMonitor globalSettings) { _ioHelper = ioHelper; _hostingEnvironment = hostingEnvironment; - _globalSettings = globalSettings.Value; + _globalSettings = globalSettings.CurrentValue; } public void Handle(UmbracoApplicationStartingNotification notification) diff --git a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Services.cs b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Services.cs index 861a05b459..10aba830b4 100644 --- a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Services.cs +++ b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Services.cs @@ -92,7 +92,7 @@ namespace Umbraco.Cms.Infrastructure.DependencyInjection factory.GetRequiredService(), factory.GetRequiredService(), factory.GetRequiredService(), - factory.GetRequiredService>(), + factory.GetRequiredService>(), factory.GetRequiredService(), factory.GetRequiredService(), factory.GetRequiredService(), diff --git a/src/Umbraco.Tests.Common/TestOptionsSnapshot.cs b/src/Umbraco.Tests.Common/TestOptionsSnapshot.cs new file mode 100644 index 0000000000..9b3cdb4b2c --- /dev/null +++ b/src/Umbraco.Tests.Common/TestOptionsSnapshot.cs @@ -0,0 +1,11 @@ +using Microsoft.Extensions.Options; + +namespace Umbraco.Cms.Tests.Common +{ + public class TestOptionsSnapshot : IOptionsSnapshot where T : class + { + public TestOptionsSnapshot(T value) => Value = value; + public T Value { get; } + public T Get(string name) => Value; + } +} diff --git a/src/Umbraco.Tests.Integration/Umbraco.Core/Packaging/CreatedPackagesRepositoryTests.cs b/src/Umbraco.Tests.Integration/Umbraco.Core/Packaging/CreatedPackagesRepositoryTests.cs index b1f3fbf847..2cbf5a6f82 100644 --- a/src/Umbraco.Tests.Integration/Umbraco.Core/Packaging/CreatedPackagesRepositoryTests.cs +++ b/src/Umbraco.Tests.Integration/Umbraco.Core/Packaging/CreatedPackagesRepositoryTests.cs @@ -9,14 +9,13 @@ using System.Linq; using System.Xml.Linq; using NUnit.Framework; using Umbraco.Cms.Core; -using Umbraco.Cms.Core.Configuration; using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Cms.Core.Hosting; using Umbraco.Cms.Core.IO; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Packaging; using Umbraco.Cms.Core.Services; -using Umbraco.Cms.Core.Strings; +using Umbraco.Cms.Tests.Common; using Umbraco.Cms.Tests.Common.Builders; using Umbraco.Cms.Tests.Common.Testing; using Umbraco.Cms.Tests.Integration.Testing; @@ -71,7 +70,7 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Packaging LocalizationService, HostingEnvironment, EntityXmlSerializer, - Microsoft.Extensions.Options.Options.Create(new GlobalSettings()), + new TestOptionsMonitor(new GlobalSettings()), MediaService, MediaTypeService, MediaFileManager, diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Core/Templates/HtmlImageSourceParserTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Core/Templates/HtmlImageSourceParserTests.cs index 4d0e643d7f..a4b0095fc0 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Core/Templates/HtmlImageSourceParserTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Core/Templates/HtmlImageSourceParserTests.cs @@ -81,7 +81,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Templates var webRoutingSettings = new WebRoutingSettings(); var publishedUrlProvider = new UrlProvider( umbracoContextAccessor, - Options.Create(webRoutingSettings), + new TestOptionsSnapshot(webRoutingSettings), new UrlProviderCollection(() => Enumerable.Empty()), new MediaUrlProviderCollection(() => new[] { mediaUrlProvider.Object }), Mock.Of()); diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Core/Templates/HtmlLocalLinkParserTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Core/Templates/HtmlLocalLinkParserTests.cs index 670f528bbd..653f3138fd 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Core/Templates/HtmlLocalLinkParserTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Core/Templates/HtmlLocalLinkParserTests.cs @@ -78,7 +78,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Templates var webRoutingSettings = new WebRoutingSettings(); var publishedUrlProvider = new UrlProvider( umbracoContextAccessor, - Microsoft.Extensions.Options.Options.Create(webRoutingSettings), + new TestOptionsSnapshot(webRoutingSettings), new UrlProviderCollection(() => new[] { contentUrlProvider.Object }), new MediaUrlProviderCollection(() => new[] { mediaUrlProvider.Object }), Mock.Of());