diff --git a/Core/Configurations/ConfigurationBuilder.cs b/Core/Configurations/ConfigurationBuilder.cs index e3c29ff..95ab656 100644 --- a/Core/Configurations/ConfigurationBuilder.cs +++ b/Core/Configurations/ConfigurationBuilder.cs @@ -23,7 +23,7 @@ namespace Core.Configurations { provider.Build(); } - //TODO: we need to come up with merge strategy + //TODO: we need to come up with merge strategy, right now the latest key-path dominates return new ConfigurationRoot(ConfigurationProviders); } @@ -33,6 +33,14 @@ namespace Core.Configurations { List _providers = []; + /// + /// Implements a string-based indexer for backwards compatibility with Microsoft.Extensions.Configuration. + /// This implementation is marked as obsolete and should be replaced with type-safe alternatives. + /// + /// The configuration key to retrieve. + /// The configuration value for the specified key. + /// Thrown when attempting to set a value, as this operation is not supported. + [Obsolete("Use type-safe configuration methods instead")] public string this[string key] { get => GetConfiguration(_providers, key); @@ -58,14 +66,10 @@ namespace Core.Configurations return value; } - - - } + public class ConfigurationRoot : Configuration, IConfigurationRoot { - List IConfiguration.ConfigurationProviders { get; set; } - public ConfigurationRoot(List configurationProviders) { ((IConfiguration)this).ConfigurationProviders = configurationProviders; diff --git a/Core/Configurations/SmartConfigProvider/SmartConfigExtension.cs b/Core/Configurations/SmartConfigProvider/SmartConfigExtension.cs index cf32907..b63bfb0 100644 --- a/Core/Configurations/SmartConfigProvider/SmartConfigExtension.cs +++ b/Core/Configurations/SmartConfigProvider/SmartConfigExtension.cs @@ -8,6 +8,13 @@ namespace Core.Configurations.SmartConfig { public static class SmartConfigExtension { + /// + /// + /// + /// + /// Name of key + /// If this is different than the default ConnectionStrings-element, then configure it. + /// public static IConfigurationBuilder AddSmartConfig(this IConfigurationBuilder builder, string configKey = "DefaultConnection", string path = null) { return builder.AddProvider(new SmartConfigProvider(builder, configKey, path)); @@ -83,7 +90,7 @@ namespace Core.Configurations.SmartConfig var pairs = configs.Select(x => new KeyValuePair(x.Key, JToken.Parse(x.Value.ToString()))); _configuration = Common.KeyValueToJson.Convert(pairs); - + } public Newtonsoft.Json.Linq.JObject Configuration() diff --git a/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs b/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs index ab48f42..921961d 100644 --- a/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs +++ b/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs @@ -7,6 +7,7 @@ using Autofac; using System.Data; using Insight.Database; using Npgsql; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Tests.ConfigurationTests { @@ -52,7 +53,9 @@ namespace Tests.ConfigurationTests // Act var actualFeature = builder.GetSection("Feature").ToObject(); +#pragma warning disable CS0618 // Type or member is obsolete var actualFeatureObsoleted = builder.GetSection("Feature").Get(); +#pragma warning restore CS0618 // Type or member is obsolete // Assert actualFeature.Should().BeEquivalentTo(expectedFeature); actualFeatureObsoleted.Should().BeEquivalentTo(expectedFeature); @@ -74,6 +77,25 @@ namespace Tests.ConfigurationTests // Assert actualFeature.Should().BeEquivalentTo(expectedFeature); } + /// + /// Testing a stupid indexer for compability with Microsoft ConfigurationBuilder + /// + [TestMethod] + public void Indexer_ShouldReturnValueAsString() + { + // Arrange + var expected = "SHA256"; + + var builder = new ConfigurationBuilder() + .AddJsonFile("appconfiguration.dev.json") + .Build(); + + // Act + var actual = builder["Authentication"]; + + // Assert + actual.Should().BeEquivalentTo(expected); + } [TestMethod] public void Get_ShouldReturnCorrectValueAsInt() { diff --git a/Tests/TestFixture.cs b/Tests/TestFixture.cs index 2aa52f8..0c9cf5a 100644 --- a/Tests/TestFixture.cs +++ b/Tests/TestFixture.cs @@ -35,7 +35,7 @@ namespace Tests { CreateContainerBuilder(); Container = ContainerBuilder.Build(); - + } @@ -69,7 +69,7 @@ namespace Tests .SingleInstance(); - builder.RegisterModule(new Core.ModuleRegistry.DbPostgreSqlModule + builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule { ConnectionString = configuration.GetConnectionString("DefaultConnection") }); @@ -86,8 +86,6 @@ namespace Tests [TestCleanup] public void CleanUp() { - //Serilog.Log.CloseAndFlush(); - Trace.Flush(); var telemetryClient = Container.Resolve(); telemetryClient.Flush(); diff --git a/Tests/appconfiguration.dev.json b/Tests/appconfiguration.dev.json index 79333a6..d78a92e 100644 --- a/Tests/appconfiguration.dev.json +++ b/Tests/appconfiguration.dev.json @@ -6,6 +6,7 @@ "ApplicationInsights": { "ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/" }, + "Authentication": "SHA256", "Feature": { "Enabled": true, "RolloutPercentage": 25,