Avoid usage of IOHelper in ConnectionStrings.cs

This commit is contained in:
Bjarke Berg
2020-03-13 12:45:22 +01:00
parent 1c03b470d9
commit 263b986ced
10 changed files with 24 additions and 29 deletions

View File

@@ -31,7 +31,7 @@ namespace Umbraco.Core.Configuration
public IContentSettings ContentSettings { get; } = new ContentSettings();
public IGlobalSettings GlobalSettings { get; } = new GlobalSettings();
public Configs Create(IIOHelper ioHelper, ILogger logger)
public Configs Create()
{
var configs = new Configs(section => ConfigurationManager.GetSection(section));
configs.Add<IGlobalSettings>(() => GlobalSettings);
@@ -41,7 +41,7 @@ namespace Umbraco.Core.Configuration
configs.Add(() => CoreDebug);
configs.Add(() => MachineKeyConfig);
configs.Add<IConnectionStrings>(() => new ConnectionStrings(ioHelper, logger));
configs.Add<IConnectionStrings>(() => new ConnectionStrings());
configs.Add<IModelsBuilderConfig>(() => new ModelsBuilderConfig());

View File

@@ -3,6 +3,7 @@ using System.Configuration;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Umbraco.Composing;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
@@ -10,14 +11,6 @@ namespace Umbraco.Core.Configuration
{
public class ConnectionStrings : IConnectionStrings
{
private readonly IIOHelper _ioHelper;
private readonly ILogger _logger;
public ConnectionStrings(IIOHelper ioHelper, ILogger logger)
{
_ioHelper = ioHelper;
_logger = logger;
}
public ConfigConnectionString this[string key]
{
@@ -29,9 +22,9 @@ namespace Umbraco.Core.Configuration
}
}
public void RemoveConnectionString(string key)
public void RemoveConnectionString(string key, IIOHelper ioHelper)
{
var fileName = _ioHelper.MapPath(string.Format("{0}/web.config", _ioHelper.Root));
var fileName = ioHelper.MapPath(string.Format("{0}/web.config", ioHelper.Root));
var xml = XDocument.Load(fileName, LoadOptions.PreserveWhitespace);
var appSettings = xml.Root.DescendantsAndSelf("appSettings").Single();
@@ -52,7 +45,7 @@ namespace Umbraco.Core.Configuration
/// <remarks>Saves the ConnectionString in the very nasty 'medium trust'-supportive way.</remarks>
/// <param name="connectionString">The connection string.</param>
/// <param name="providerName">The provider name.</param>
public void SaveConnectionString(string connectionString, string providerName)
public void SaveConnectionString(string connectionString, string providerName, IIOHelper ioHelper)
{
if (connectionString == null) throw new ArgumentNullException(nameof(connectionString));
@@ -62,7 +55,7 @@ namespace Umbraco.Core.Configuration
var fileSource = "web.config";
var fileName = _ioHelper.MapPath(_ioHelper.Root +"/" + fileSource);
var fileName = ioHelper.MapPath(ioHelper.Root +"/" + fileSource);
var xml = XDocument.Load(fileName, LoadOptions.PreserveWhitespace);
if (xml.Root == null) throw new Exception($"Invalid {fileSource} file (no root).");
@@ -75,7 +68,7 @@ namespace Umbraco.Core.Configuration
if (configSourceAttribute != null)
{
fileSource = configSourceAttribute.Value;
fileName = _ioHelper.MapPath(_ioHelper.Root + "/" + fileSource);
fileName = ioHelper.MapPath(ioHelper.Root + "/" + fileSource);
if (!File.Exists(fileName))
throw new Exception($"Invalid configSource \"{fileSource}\" (no such file).");
@@ -103,9 +96,9 @@ namespace Umbraco.Core.Configuration
}
// save
_logger.Info<ConnectionStrings>("Saving connection string to {ConfigFile}.", fileSource);
Current.Logger.Info<ConnectionStrings>("Saving connection string to {ConfigFile}.", fileSource);
xml.Save(fileName, SaveOptions.DisableFormatting);
_logger.Info<ConnectionStrings>("Saved connection string to {ConfigFile}.", fileSource);
Current.Logger.Info<ConnectionStrings>("Saved connection string to {ConfigFile}.", fileSource);
}
private static void AddOrUpdateAttribute(XElement element, string name, string value)

View File

@@ -5,6 +5,6 @@ namespace Umbraco.Core.Configuration
{
public interface IConfigsFactory
{
Configs Create(IIOHelper ioHelper, ILogger logger);
Configs Create();
}
}

View File

@@ -1,3 +1,5 @@
using Umbraco.Core.IO;
namespace Umbraco.Core.Configuration
{
public interface IConnectionStrings
@@ -7,7 +9,7 @@ namespace Umbraco.Core.Configuration
get;
}
void RemoveConnectionString(string umbracoConnectionName);
void SaveConnectionString(string connectionString, string providerName);
void RemoveConnectionString(string umbracoConnectionName, IIOHelper ioHelper);
void SaveConnectionString(string connectionString, string providerName, IIOHelper ioHelper);
}
}

View File

@@ -65,7 +65,7 @@ namespace Umbraco.Web.Install.InstallSteps
// Remove legacy umbracoDbDsn configuration setting if it exists and connectionstring also exists
if (databaseSettings != null)
{
connectionStrings.RemoveConnectionString(Constants.System.UmbracoConnectionName);
connectionStrings.RemoveConnectionString(Constants.System.UmbracoConnectionName, ioHelper);
}
else
{

View File

@@ -146,7 +146,7 @@ namespace Umbraco.Core.Migrations.Install
private void ConfigureEmbeddedDatabaseConnection(IUmbracoDatabaseFactory factory, IIOHelper ioHelper)
{
_connectionStrings.SaveConnectionString(EmbeddedDatabaseConnectionString, Constants.DbProviderNames.SqlCe);
_connectionStrings.SaveConnectionString(EmbeddedDatabaseConnectionString, Constants.DbProviderNames.SqlCe, ioHelper);
var path = Path.Combine(ioHelper.GetRootDirectorySafe(), "App_Data", "Umbraco.sdf");
if (File.Exists(path) == false)
@@ -169,7 +169,7 @@ namespace Umbraco.Core.Migrations.Install
{
const string providerName = Constants.DbProviderNames.SqlServer;
_connectionStrings.SaveConnectionString(connectionString, providerName);
_connectionStrings.SaveConnectionString(connectionString, providerName, _ioHelper);
_databaseFactory.Configure(connectionString, providerName);
}
@@ -185,7 +185,7 @@ namespace Umbraco.Core.Migrations.Install
{
var connectionString = GetDatabaseConnectionString(server, databaseName, user, password, databaseProvider, out var providerName);
_connectionStrings.SaveConnectionString(connectionString, providerName);
_connectionStrings.SaveConnectionString(connectionString, providerName, _ioHelper);
_databaseFactory.Configure(connectionString, providerName);
}
@@ -216,7 +216,7 @@ namespace Umbraco.Core.Migrations.Install
public void ConfigureIntegratedSecurityDatabaseConnection(string server, string databaseName)
{
var connectionString = GetIntegratedSecurityDatabaseConnectionString(server, databaseName);
_connectionStrings.SaveConnectionString(connectionString, Constants.DbProviderNames.SqlServer);
_connectionStrings.SaveConnectionString(connectionString, Constants.DbProviderNames.SqlServer, _ioHelper);
_databaseFactory.Configure(connectionString, Constants.DbProviderNames.SqlServer);
}

View File

@@ -50,7 +50,7 @@ namespace Umbraco.Tests.Common
public Configs GetConfigs()
{
return GetConfigsFactory().Create(IOHelper, Mock.Of<ILogger>());
return GetConfigsFactory().Create();
}
public IRuntimeState GetRuntimeState()
{

View File

@@ -96,7 +96,7 @@ namespace Umbraco.Tests.Runtimes
private static Configs GetConfigs()
{
var configs = new ConfigsFactory().Create(_ioHelper, _logger);
var configs = new ConfigsFactory().Create();
configs.Add(SettingsForTests.GetDefaultGlobalSettings);
configs.Add(SettingsForTests.GenerateMockContentSettings);
configs.Add(SettingsForTests.GetDefaultHostingSettings);

View File

@@ -45,7 +45,7 @@ namespace Umbraco.Web.BackOffice.AspNetCore
var hostingEnvironment = new AspNetCoreHostingEnvironment(hostingSettings, webHostEnvironment, httpContextAccessor, hostApplicationLifetime);
var ioHelper = new IOHelper(hostingEnvironment, globalSettings);
var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment, new AspNetCoreSessionIdResolver(httpContextAccessor), () => services.BuildServiceProvider().GetService<IRequestCache>(), coreDebug, ioHelper, new AspNetCoreMarchal());
var configs = configFactory.Create(ioHelper, logger);
var configs = configFactory.Create();
var backOfficeInfo = new AspNetCoreBackOfficeInfo(globalSettings);
var profiler = new LogProfiler(logger);

View File

@@ -40,7 +40,7 @@ namespace Umbraco.Web
var hostingEnvironment = new AspNetHostingEnvironment(hostingSettings);
var ioHelper = new IOHelper(hostingEnvironment, globalSettings);
var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment, new AspNetSessionManager(), () => _factory?.GetInstance<IRequestCache>(), coreDebug, ioHelper, new FrameworkMarchal());
var configs = configFactory.Create(ioHelper, logger);
var configs = configFactory.Create();
var backOfficeInfo = new AspNetBackOfficeInfo(globalSettings, ioHelper, logger, configFactory.WebRoutingSettings);
var profiler = new LogProfiler(logger);