Skip to content

Commit 037fd75

Browse files
committedApr 17, 2025·
避免重复的 ChangeToken.OnChange
1 parent e3f847a commit 037fd75

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed
 

‎Demo/Program.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
using Microsoft.Extensions.Primitives;
2+
13
var builder = WebApplication.CreateBuilder(args);
24
builder.Services.AddControllers();
35
builder.Services.AddOpenApi();
46
builder.Configuration.AddUserSecrets("personal_access_tokens");
57
builder.Configuration.AddGitRepository(cfg => cfg.WithGitLab()
6-
.WithHostUrl("https://git.uixe.net/")
7-
.WithRepositoryPath("uixe/stdlanedevctlsvr")
8+
.WithRepositoryPath("IoTSharp/gitlabcfg")
89
.WithAuthenticationToken(builder.Configuration.GetValue<string>("personal_access_tokens"))
9-
.WithFileName($"{Environment.GetEnvironmentVariable("UIXEID")}/appsettings.{builder.Environment.EnvironmentName}.json")
10+
.WithFileName($"appsettings.json")
1011
.WithCache($"{builder.Environment.ContentRootPath}{System.IO.Path.DirectorySeparatorChar}appsettings.{builder.Environment.EnvironmentName}.json")
1112
);
1213

@@ -18,6 +19,22 @@
1819
app.MapOpenApi();
1920
}
2021
Console.WriteLine($"abc={app.Configuration.GetValue<string>("abc")}");
21-
app.UseAuthorization();
22+
23+
ChangeToken.OnChange(() => app.Configuration.GetReloadToken(), () =>
24+
{
25+
Console.WriteLine($"abc={app.Configuration.GetValue<string>("abc")}");
26+
var settings = app.Configuration.Get<AppSettings>();
27+
foreach (var item in settings?.Menus)
28+
{
29+
Console.WriteLine($"Menu={item}");
30+
}
31+
});
32+
app.UseAuthorization();
2233
app.MapControllers();
23-
app.Run();
34+
app.Run();
35+
36+
public class AppSettings
37+
{
38+
public List<string> Menus { get; set; } = new List<string>();
39+
40+
}

‎Demo/appsettings.Development.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
"Microsoft.AspNetCore": "Warning"
66
}
77
},
8-
"abc": 12223467,
9-
"sdfsd": "asdfsadfasd"
8+
"sdfsd": "asdfsadfasd",
9+
"abc": 1232323223,
10+
"Menus": [
11+
"App",
12+
"abc",
13+
"fdsfsdfsd",
14+
"sadfsdfsd12",
15+
"sdfasdfasds"
16+
]
1017
}

‎Extensions.Configuration.GitRepository/GitRepositoryConfigurationProvider.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ private void SyncGitRepoFile()
6464
{
6565
_jsonData = ReadCache();
6666
}
67-
ReadGitRepo( ref _jsonData);
68-
if (_jsonData != null)
67+
bool result= ReadGitRepo( ref _jsonData);
68+
if (_jsonData != null && result)
6969
{
70-
7170
Data = JsonConfigurationFileParser.Parse(_jsonData);
7271
OnReload();
7372
}
@@ -84,8 +83,9 @@ private void SyncGitRepoFile()
8483
}
8584
}
8685

87-
private void ReadGitRepo( ref JsonDocument _jsonData)
86+
private bool ReadGitRepo( ref JsonDocument _jsonData)
8887
{
88+
bool result = false;
8989
try
9090
{
9191
if (__gitRepo.FileExists(_options.FileName))
@@ -97,6 +97,7 @@ private void ReadGitRepo( ref JsonDocument _jsonData)
9797
if (_jsonData != null)
9898
{
9999
SaveCache(fileContent);
100+
result = true;
100101
}
101102
}
102103
else
@@ -109,6 +110,7 @@ private void ReadGitRepo( ref JsonDocument _jsonData)
109110
string _json = _jn.ToJsonString(new JsonSerializerOptions() { WriteIndented = true });
110111
SaveCache(_json);
111112
_jsonData = JsonDocument.Parse(_json);
113+
result = true;
112114

113115
}
114116
}
@@ -126,6 +128,7 @@ private void ReadGitRepo( ref JsonDocument _jsonData)
126128
Console.WriteLine(ex1.ToString());
127129

128130
}
131+
return result;
129132
}
130133

131134
private JsonDocument ReadCache()

0 commit comments

Comments
 (0)
Please sign in to comment.