From dcc3f0deae9da4c3a41a0dc68fa6881bc951100c Mon Sep 17 00:00:00 2001 From: Tom Gardham-Pallister Date: Thu, 20 Sep 2018 18:33:00 +0100 Subject: [PATCH] #559 +semver: breaking always use environment when working out AddOcelot on builder --- docs/features/configuration.rst | 18 ++++++++++++++- .../ConfigurationBuilderExtensions.cs | 4 ++-- .../ConfigurationBuilderExtensionsTests.cs | 23 ++++++++++++------- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/docs/features/configuration.rst b/docs/features/configuration.rst index 4790afc5..dfe30c5a 100644 --- a/docs/features/configuration.rst +++ b/docs/features/configuration.rst @@ -107,7 +107,7 @@ Instead of adding the configuration directly e.g. AddJsonFile("ocelot.json") you .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) .AddJsonFile("appsettings.json", true, true) .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) - .AddOcelot() + .AddOcelot(hostingContext.HostingEnvironment) .AddEnvironmentVariables(); }) @@ -117,6 +117,22 @@ The way Ocelot merges the files is basically load them, loop over them, add any At the moment there is no validation at this stage it only happens when Ocelot validates the final merged configuration. This is something to be aware of when you are investigating problems. I would advise always checking what is in ocelot.json if you have any problems. +You can also give Ocelot a specific path to look in for the configuration files like below. + +.. code-block:: csharp + + .ConfigureAppConfiguration((hostingContext, config) => + { + config + .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) + .AddJsonFile("appsettings.json", true, true) + .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) + .AddOcelot("/foo/bar", hostingContext.HostingEnvironment) + .AddEnvironmentVariables(); + }) + +Ocelot needs the HostingEnvironment so it know's to exclude anything environment specific from the algorithm. + Store configuration in consul ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs b/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs index af6351ea..02b5a17b 100644 --- a/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs +++ b/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs @@ -29,12 +29,12 @@ namespace Ocelot.DependencyInjection return builder; } - public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, IHostingEnvironment env = null) + public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, IHostingEnvironment env) { return builder.AddOcelot(".", env); } - public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, string folder, IHostingEnvironment env = null) + public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, string folder, IHostingEnvironment env) { const string primaryConfigFile = "ocelot.json"; diff --git a/test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs b/test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs index da2bc341..465c9173 100644 --- a/test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs +++ b/test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs @@ -22,9 +22,12 @@ private FileConfiguration _reRouteB; private FileConfiguration _aggregate; private FileConfiguration _envSpecific; + private Mock _hostingEnvironment; + public ConfigurationBuilderExtensionsTests() { + _hostingEnvironment = new Mock(); // Clean up config files before each test var subConfigFiles = new DirectoryInfo(".").GetFiles("ocelot.*.json"); @@ -47,7 +50,8 @@ public void should_merge_files() { this.Given(_ => GivenMultipleConfigurationFiles("", false)) - .When(_ => WhenIAddOcelotConfiguration(false)) + .And(_ => GivenTheEnvironmentIs(null)) + .When(_ => WhenIAddOcelotConfiguration()) .Then(_ => ThenTheConfigsAreMerged()) .BDDfy(); } @@ -56,7 +60,8 @@ public void should_merge_files_except_env() { this.Given(_ => GivenMultipleConfigurationFiles("", true)) - .When(_ => WhenIAddOcelotConfiguration(true)) + .And(_ => GivenTheEnvironmentIs("Env")) + .When(_ => WhenIAddOcelotConfiguration()) .Then(_ => ThenTheConfigsAreMerged()) .And(_ => NotContainsEnvSpecificConfig()) .BDDfy(); @@ -241,14 +246,16 @@ } } - private void WhenIAddOcelotConfiguration(bool addEnv) + private void GivenTheEnvironmentIs(string env) + { + _hostingEnvironment.SetupGet(x => x.EnvironmentName).Returns(env); + } + + private void WhenIAddOcelotConfiguration() { IConfigurationBuilder builder = new ConfigurationBuilder(); - var hostingEnvironment = new Mock(); - hostingEnvironment.SetupGet(x => x.EnvironmentName).Returns(addEnv ? "Env" : null); - - builder.AddOcelot(hostingEnvironment.Object); + builder.AddOcelot(_hostingEnvironment.Object); _configRoot = builder.Build(); } @@ -256,7 +263,7 @@ private void WhenIAddOcelotConfigurationWithSpecificFolder(string folder) { IConfigurationBuilder builder = new ConfigurationBuilder(); - builder.AddOcelot(folder); + builder.AddOcelot(folder, _hostingEnvironment.Object); _configRoot = builder.Build(); }