Skip to content

Commit

Permalink
Respect reloadOnChange in UserSecrets in DefaultBuilder (#43807)
Browse files Browse the repository at this point in the history
* Respect reloadOnChange in UserSecrets in DefaultBuilder
  • Loading branch information
galakt committed Oct 28, 2020
1 parent 9cce94a commit 6a5a78b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libraries/Microsoft.Extensions.Hosting/src/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static IHostBuilder CreateDefaultBuilder(string[] args)
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
if (appAssembly != null)
{
config.AddUserSecrets(appAssembly, optional: true);
config.AddUserSecrets(appAssembly, optional: true, reloadOnChange: reloadOnChange);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Extensions.Configuration.UserSecrets;
using Xunit;

[assembly: ActiveIssue("https://github.com/dotnet/runtime/issues/37669", TestPlatforms.Browser)]
[assembly: UserSecretsId("Microsoft.Extensions.Hosting.Unit.Tests")]
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;
Expand Down Expand Up @@ -203,6 +205,52 @@ string SaveRandomConfig()
Assert.Equal(dynamicConfigMessage2, config["Hello"]); // Config DID reload from disk
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34580", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public async Task CreateDefaultBuilder_SecretsDoesReload()
{
var secretId = Assembly.GetExecutingAssembly().GetName().Name;
var reloadFlagConfig = new Dictionary<string, string>() { { "hostbuilder:reloadConfigOnChange", "true" } };
var secretPath = PathHelper.GetSecretsPathFromSecretsId(secretId);

var secretFileInfo = new FileInfo(secretPath);
Directory.CreateDirectory(secretFileInfo.Directory.FullName);

string SaveRandomSecret()
{
var newMessage = $"Hello ASP.NET Core: {Guid.NewGuid():N}";
File.WriteAllText(secretPath, $"{{ \"Hello\": \"{newMessage}\" }}");
return newMessage;
}

string dynamicSecretMessage1 = SaveRandomSecret();

var host = Host.CreateDefaultBuilder(new[] { "environment=Development", $"applicationName={secretId}" })
.ConfigureHostConfiguration(builder =>
{
builder.AddInMemoryCollection(reloadFlagConfig);
})
.Build();

var config = host.Services.GetRequiredService<IConfiguration>();

Assert.Equal(dynamicSecretMessage1, config["Hello"]);

string dynamicSecretMessage2 = SaveRandomSecret();

var configReloadedCancelTokenSource = new CancellationTokenSource();
var configReloadedCancelToken = configReloadedCancelTokenSource.Token;

config.GetReloadToken().RegisterChangeCallback(o =>
{
configReloadedCancelTokenSource.Cancel();
}, null);
// Wait for up to 1 minute, if config reloads at any time, cancel the wait.
await Task.WhenAny(Task.Delay(TimeSpan.FromMinutes(1), configReloadedCancelToken)); // Task.WhenAny ignores the task throwing on cancellation.
Assert.NotEqual(dynamicSecretMessage1, dynamicSecretMessage2); // Messages are different.
Assert.Equal(dynamicSecretMessage2, config["Hello"]);
}

internal class ServiceA { }

internal class ServiceB
Expand Down

0 comments on commit 6a5a78b

Please sign in to comment.