-
Notifications
You must be signed in to change notification settings - Fork 25.3k
/
Startup.cs
75 lines (65 loc) · 2.24 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Primitives;
using static ChangeTokenSample.Utilities.Utilities;
namespace ChangeTokenSample
{
public class Startup
{
private byte[] _appsettingsHash = new byte[20];
private byte[] _appsettingsEnvHash = new byte[20];
public void ConfigureServices(IServiceCollection services)
{
#region snippet1
services.AddSingleton<IConfigurationMonitor, ConfigurationMonitor>();
#endregion
#region snippet4
services.AddMemoryCache();
services.AddSingleton<FileService>();
#endregion
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IConfiguration config, IWebHostEnvironment env)
{
#region snippet2
ChangeToken.OnChange(
() => config.GetReloadToken(),
(state) => InvokeChanged(state),
env);
#endregion
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
#region snippet3
private void InvokeChanged(IWebHostEnvironment env)
{
byte[] appsettingsHash = ComputeHash("appSettings.json");
byte[] appsettingsEnvHash =
ComputeHash($"appSettings.{env.EnvironmentName}.json");
if (!_appsettingsHash.SequenceEqual(appsettingsHash) ||
!_appsettingsEnvHash.SequenceEqual(appsettingsEnvHash))
{
_appsettingsHash = appsettingsHash;
_appsettingsEnvHash = appsettingsEnvHash;
WriteConsole("Configuration changed (Simple Startup Change Token)");
}
}
#endregion
}
}