#559 +semver: breaking always use environment when working out AddOcelot on builder

This commit is contained in:
Tom Gardham-Pallister 2018-09-20 18:33:00 +01:00
parent 7c0aa6f97e
commit dcc3f0deae
3 changed files with 34 additions and 11 deletions

View File

@ -107,7 +107,7 @@ Instead of adding the configuration directly e.g. AddJsonFile("ocelot.json") you
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", true, true) .AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
.AddOcelot() .AddOcelot(hostingContext.HostingEnvironment)
.AddEnvironmentVariables(); .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. 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 Store configuration in consul
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -29,12 +29,12 @@ namespace Ocelot.DependencyInjection
return builder; 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); 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"; const string primaryConfigFile = "ocelot.json";

View File

@ -22,9 +22,12 @@
private FileConfiguration _reRouteB; private FileConfiguration _reRouteB;
private FileConfiguration _aggregate; private FileConfiguration _aggregate;
private FileConfiguration _envSpecific; private FileConfiguration _envSpecific;
private Mock<IHostingEnvironment> _hostingEnvironment;
public ConfigurationBuilderExtensionsTests() public ConfigurationBuilderExtensionsTests()
{ {
_hostingEnvironment = new Mock<IHostingEnvironment>();
// Clean up config files before each test // Clean up config files before each test
var subConfigFiles = new DirectoryInfo(".").GetFiles("ocelot.*.json"); var subConfigFiles = new DirectoryInfo(".").GetFiles("ocelot.*.json");
@ -47,7 +50,8 @@
public void should_merge_files() public void should_merge_files()
{ {
this.Given(_ => GivenMultipleConfigurationFiles("", false)) this.Given(_ => GivenMultipleConfigurationFiles("", false))
.When(_ => WhenIAddOcelotConfiguration(false)) .And(_ => GivenTheEnvironmentIs(null))
.When(_ => WhenIAddOcelotConfiguration())
.Then(_ => ThenTheConfigsAreMerged()) .Then(_ => ThenTheConfigsAreMerged())
.BDDfy(); .BDDfy();
} }
@ -56,7 +60,8 @@
public void should_merge_files_except_env() public void should_merge_files_except_env()
{ {
this.Given(_ => GivenMultipleConfigurationFiles("", true)) this.Given(_ => GivenMultipleConfigurationFiles("", true))
.When(_ => WhenIAddOcelotConfiguration(true)) .And(_ => GivenTheEnvironmentIs("Env"))
.When(_ => WhenIAddOcelotConfiguration())
.Then(_ => ThenTheConfigsAreMerged()) .Then(_ => ThenTheConfigsAreMerged())
.And(_ => NotContainsEnvSpecificConfig()) .And(_ => NotContainsEnvSpecificConfig())
.BDDfy(); .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(); IConfigurationBuilder builder = new ConfigurationBuilder();
var hostingEnvironment = new Mock<IHostingEnvironment>(); builder.AddOcelot(_hostingEnvironment.Object);
hostingEnvironment.SetupGet(x => x.EnvironmentName).Returns(addEnv ? "Env" : null);
builder.AddOcelot(hostingEnvironment.Object);
_configRoot = builder.Build(); _configRoot = builder.Build();
} }
@ -256,7 +263,7 @@
private void WhenIAddOcelotConfigurationWithSpecificFolder(string folder) private void WhenIAddOcelotConfigurationWithSpecificFolder(string folder)
{ {
IConfigurationBuilder builder = new ConfigurationBuilder(); IConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddOcelot(folder); builder.AddOcelot(folder, _hostingEnvironment.Object);
_configRoot = builder.Build(); _configRoot = builder.Build();
} }