Fixed issues where RuntimeState.Level was used in ctors. This is not a good idea, as these ctors are executed before the level are determined.

This commit is contained in:
Bjarke Berg
2021-05-07 07:40:08 +02:00
parent cb01e11586
commit 145a0fabe7
2 changed files with 12 additions and 8 deletions

View File

@@ -10,8 +10,9 @@ namespace Umbraco.Cms.Core.PublishedCache
public class DefaultCultureAccessor : IDefaultCultureAccessor
{
private readonly ILocalizationService _localizationService;
private readonly IRuntimeState _runtimeState;
private readonly IOptions<GlobalSettings> _options;
private readonly RuntimeLevel _runtimeLevel;
/// <summary>
/// Initializes a new instance of the <see cref="DefaultCultureAccessor"/> class.
@@ -19,12 +20,13 @@ namespace Umbraco.Cms.Core.PublishedCache
public DefaultCultureAccessor(ILocalizationService localizationService, IRuntimeState runtimeState, IOptions<GlobalSettings> options)
{
_localizationService = localizationService;
_runtimeState = runtimeState;
_options = options;
_runtimeLevel = runtimeState.Level;
}
/// <inheritdoc />
public string DefaultCulture => _runtimeLevel == RuntimeLevel.Run
public string DefaultCulture => _runtimeState.Level == RuntimeLevel.Run
? _localizationService.GetDefaultLanguageIsoCode() ?? "" // fast
: _options.Value.DefaultUILanguage; // default for install and upgrade, when the service is n/a
}

View File

@@ -24,23 +24,25 @@ namespace Umbraco.Cms.Core.Services.Implement
/// </summary>
internal class UserService : RepositoryService, IUserService
{
private readonly IRuntimeState _runtimeState;
private readonly IUserRepository _userRepository;
private readonly IUserGroupRepository _userGroupRepository;
private readonly GlobalSettings _globalSettings;
private readonly bool _isUpgrading;
private readonly ILogger<UserService> _logger;
public UserService(IScopeProvider provider, ILoggerFactory loggerFactory, IEventMessagesFactory eventMessagesFactory, IRuntimeState runtimeState,
IUserRepository userRepository, IUserGroupRepository userGroupRepository, IOptions<GlobalSettings> globalSettings)
: base(provider, loggerFactory, eventMessagesFactory)
{
_runtimeState = runtimeState;
_userRepository = userRepository;
_userGroupRepository = userGroupRepository;
_globalSettings = globalSettings.Value;
_isUpgrading = runtimeState.Level == RuntimeLevel.Install || runtimeState.Level == RuntimeLevel.Upgrade;
_logger = loggerFactory.CreateLogger<UserService>();
}
private bool IsUpgrading => _runtimeState.Level == RuntimeLevel.Install || _runtimeState.Level == RuntimeLevel.Upgrade;
#region Implementation of IMembershipUserService
/// <summary>
@@ -205,7 +207,7 @@ namespace Umbraco.Cms.Core.Services.Implement
// currently kinda accepting anything on upgrade, but that won't deal with all cases
// so we need to do it differently, see the custom UmbracoPocoDataBuilder which should
// be better BUT requires that the app restarts after the upgrade!
if (_isUpgrading)
if (IsUpgrading)
{
//NOTE: this will not be cached
return _userRepository.GetByUsername(username, includeSecurityData: false);
@@ -305,7 +307,7 @@ namespace Umbraco.Cms.Core.Services.Implement
catch (DbException ex)
{
// if we are upgrading and an exception occurs, log and swallow it
if (_isUpgrading == false) throw;
if (IsUpgrading == false) throw;
_logger.LogWarning(ex, "An error occurred attempting to save a user instance during upgrade, normally this warning can be ignored");
@@ -681,7 +683,7 @@ namespace Umbraco.Cms.Core.Services.Implement
// currently kinda accepting anything on upgrade, but that won't deal with all cases
// so we need to do it differently, see the custom UmbracoPocoDataBuilder which should
// be better BUT requires that the app restarts after the upgrade!
if (_isUpgrading)
if (IsUpgrading)
{
//NOTE: this will not be cached
return _userRepository.Get(id, includeSecurityData: false);