Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect reloadOnChange in UserSecrets in DefaultBuilder #43807

Merged
merged 3 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")]
eerhardt marked this conversation as resolved.
Show resolved Hide resolved
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.
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
Assert.NotEqual(dynamicSecretMessage1, dynamicSecretMessage2); // Messages are different.
Assert.Equal(dynamicSecretMessage2, config["Hello"]);
}

internal class ServiceA { }

internal class ServiceB
Expand Down