Skip to content

Commit dd4ebb0

Browse files
committed
refactor Announcement to use with DI
1 parent f20d656 commit dd4ebb0

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

DevProxy/Announcement.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
namespace DevProxy;
88

9-
static class Announcement
9+
internal class Announcement(HttpClient httpClient)
1010
{
1111
private static readonly Uri announcementUrl = new("https://aka.ms/devproxy/announcement");
12+
private readonly HttpClient _httpClient = httpClient;
1213

13-
public static async Task ShowAsync()
14+
public async Task ShowAsync()
1415
{
1516
var announcement = await GetAsync();
1617
if (!string.IsNullOrEmpty(announcement))
@@ -22,12 +23,11 @@ public static async Task ShowAsync()
2223
}
2324
}
2425

25-
public static async Task<string?> GetAsync()
26+
private async Task<string?> GetAsync()
2627
{
2728
try
2829
{
29-
using var client = new HttpClient();
30-
return await client.GetStringAsync(announcementUrl);
30+
return await _httpClient.GetStringAsync(announcementUrl);
3131
}
3232
catch
3333
{

DevProxy/Extensions/IServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static IServiceCollection AddApplicationServices(
4545
.AddSingleton<UpdateNotification>()
4646
.AddSingleton<ProxyEngine>()
4747
.AddSingleton<DevProxyCommand>()
48+
.AddSingleton<Announcement>()
4849
.AddHttpClient();
4950

5051
_ = services.AddPlugins(configuration, options);

DevProxy/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ static WebApplication BuildApplication(string[] args, DevProxyConfigOptions opti
3232

3333
return app;
3434
}
35-
_ = Announcement.ShowAsync();
3635

3736
var options = new DevProxyConfigOptions();
3837
options.ParseOptions(args);
3938
var app = BuildApplication(args, options);
4039

40+
var announcement = app.Services.GetRequiredService<Announcement>();
41+
_ = announcement.ShowAsync();
42+
4143
var devProxyCommand = app.Services.GetRequiredService<DevProxyCommand>();
4244
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
4345

0 commit comments

Comments
 (0)