Skip to content

Commit e08d803

Browse files
Upgrade to .NET 8.0 and improve code readability
Updated project files to target .NET 8.0 across multiple components. Refactored constructors in several classes to use field initializers for better readability. Added a new `InvalidUrl` property in `HttpClientSamples.cs`. Enhanced list initialization syntax in `Formula1.cs` and improved tuple initialization in `GenerateHtml.cs`. Updated command line arguments in `launchSettings.json` and fixed a typo in `Receiver.cs`. Reformatted logger initialization in `Program.cs` and updated package references to their latest versions for compatibility with .NET 8.0.
1 parent 48acd79 commit e08d803

23 files changed

+71
-114
lines changed

2_Libs/Networking/DnsLookup/DnsLookup.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<OutputType>Exe</OutputType>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>

2_Libs/Networking/HttpClientSample/FaultHandlingSample.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
using Microsoft.Extensions.Logging;
22
using Microsoft.Extensions.Options;
33

4-
class FaultHandlingSample
4+
class FaultHandlingSample(
5+
IOptions<HttpClientSamplesOptions> options,
6+
HttpClient httpClient,
7+
ILogger<HttpClientSamples> logger)
58
{
6-
private readonly ILogger _logger;
7-
private readonly HttpClient _httpClient;
8-
private readonly string _url;
9-
10-
public FaultHandlingSample(
11-
IOptions<HttpClientSamplesOptions> options,
12-
HttpClient httpClient,
13-
ILogger<HttpClientSamples> logger)
14-
{
15-
_logger = logger;
16-
_url = options.Value.InvalidUrl ?? "localhost:5052";
17-
_httpClient = httpClient;
18-
}
9+
private readonly ILogger _logger = logger;
10+
private readonly HttpClient _httpClient = httpClient;
11+
private readonly string _url = options.Value.InvalidUrl ?? "localhost:5052";
1912

2013
public async Task RunAsync()
2114
{
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
using Microsoft.Extensions.Logging;
22

3-
public class HttpClientFactorySamples
3+
public class HttpClientFactorySamples(
4+
IHttpClientFactory httpClientFactory,
5+
ILogger<HttpClientFactorySamples> logger)
46
{
5-
private readonly HttpClient _httpClient;
6-
private readonly ILogger _logger;
7-
public HttpClientFactorySamples(
8-
IHttpClientFactory httpClientFactory,
9-
ILogger<HttpClientFactorySamples> logger)
10-
{
11-
_httpClient = httpClientFactory.CreateClient("cni");
12-
_logger = logger;
13-
}
7+
private readonly HttpClient _httpClient = httpClientFactory.CreateClient("cni");
8+
private readonly ILogger _logger = logger;
149
}

2_Libs/Networking/HttpClientSample/HttpClientSample.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<OutputType>Exe</OutputType>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
12-
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
13-
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.0" />
11+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
12+
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
13+
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.12" />
1414
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />
1515
</ItemGroup>
1616

2_Libs/Networking/HttpClientSample/HttpClientSampleWithMessageHandler.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
using Microsoft.Extensions.Logging;
22

3-
public class HttpClientSampleWithMessageHandler
3+
public class HttpClientSampleWithMessageHandler(
4+
HttpClient httpClient,
5+
ILogger<HttpClientSampleWithMessageHandler> logger)
46
{
5-
private readonly HttpClient _httpClient;
6-
private readonly ILogger _logger;
7-
public HttpClientSampleWithMessageHandler(
8-
HttpClient httpClient,
9-
ILogger<HttpClientSampleWithMessageHandler> logger)
10-
{
11-
_httpClient = httpClient;
12-
_logger = logger;
13-
}
7+
private readonly HttpClient _httpClient = httpClient;
8+
private readonly ILogger _logger = logger;
149

1510
public async Task UseMessageHandlerAsync()
1611
{

2_Libs/Networking/HttpClientSample/HttpClientSamples.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,15 @@ public record HttpClientSamplesOptions
99
public string? InvalidUrl { get; init; }
1010
}
1111

12-
public class HttpClientSamples
12+
public class HttpClientSamples(
13+
IOptions<HttpClientSamplesOptions> options,
14+
HttpClient httpClient,
15+
ILogger<HttpClientSamples> logger)
1316
{
14-
private readonly ILogger _logger;
15-
private readonly HttpClient _httpClient;
16-
private readonly string _url;
17-
private readonly string _invalidUrl;
18-
19-
public HttpClientSamples(
20-
IOptions<HttpClientSamplesOptions> options,
21-
HttpClient httpClient,
22-
ILogger<HttpClientSamples> logger)
23-
{
24-
_url = options.Value.Url ?? "https://localhost:5020";
25-
_invalidUrl = options.Value.InvalidUrl ?? "https://localhost1:5020";
26-
_httpClient = httpClient;
27-
_logger = logger;
28-
}
17+
private readonly ILogger _logger = logger;
18+
private readonly HttpClient _httpClient = httpClient;
19+
private readonly string _url = options.Value.Url ?? "https://localhost:5020";
20+
private readonly string _invalidUrl = options.Value.InvalidUrl ?? "https://localhost1:5020";
2921

3022
public async Task SimpleGetRequestAsync()
3123
{
@@ -106,7 +98,7 @@ public async Task UseHttp2()
10698
HttpRequestMessage request2 = new(HttpMethod.Get, "/api/racers");
10799
request2.Version = new Version("1.1");
108100

109-
Stopwatch stopwatch = new Stopwatch();
101+
Stopwatch stopwatch = new();
110102
stopwatch.Start();
111103
Task<HttpResponseMessage> t1 = _httpClient.SendAsync(request1);
112104
Task<HttpResponseMessage> t2 = _httpClient.SendAsync(request2); ;

2_Libs/Networking/HttpClientSample/NamedClientsSample.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
using Microsoft.Extensions.Logging;
22
using Microsoft.Extensions.Options;
33

4-
class NamedClientSample
4+
class NamedClientSample(
5+
IOptions<HttpClientSamplesOptions> options,
6+
IHttpClientFactory httpClientFactory,
7+
ILogger<HttpClientSamples> logger)
58
{
6-
private readonly ILogger _logger;
7-
private readonly HttpClient _httpClient;
8-
private readonly string _url;
9-
10-
public NamedClientSample(
11-
IOptions<HttpClientSamplesOptions> options,
12-
IHttpClientFactory httpClientFactory,
13-
ILogger<HttpClientSamples> logger)
14-
{
15-
_logger = logger;
16-
_url = options.Value.InvalidUrl ?? "localhost:5052";
17-
_httpClient = httpClientFactory.CreateClient("racersClient");
18-
}
9+
private readonly ILogger _logger = logger;
10+
private readonly HttpClient _httpClient = httpClientFactory.CreateClient("racersClient");
11+
private readonly string _url = options.Value.InvalidUrl ?? "localhost:5052";
1912

2013
public async Task RunAsync()
2114
{

2_Libs/Networking/HttpClientSample/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"HttpClientSample": {
44
"commandName": "Project",
5-
"commandLineArgs": "messagehandler"
5+
"commandLineArgs": "retry"
66
}
77
}
88
}

2_Libs/Networking/HttpServerSample/Formula1.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ public class Formula1
55
private List<Racer>? _racers;
66
public IEnumerable<Racer> GetChampions() => _racers ??= InitializeRacers();
77

8-
private static List<Racer> InitializeRacers() => new()
9-
{
8+
private static List<Racer> InitializeRacers() =>
9+
[
1010
new("Nino", "Farina", "Italy", 33, 5, new[] { 1950 }, new[] { "Alfa Romeo" }),
1111
new("Alberto", "Ascari", "Italy", 32, 13, new[] { 1952, 1953 }, new[] { "Ferrari" }),
1212
new("Juan Manuel", "Fangio", "Argentina", 51, 24, new int[] { 1951, 1954, 1955, 1956, 1957 }, new string[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" }),
@@ -41,5 +41,5 @@ public class Formula1
4141
new("Sebastian", "Vettel", "Germany", 296, 53, new int[] { 2010, 2011, 2012, 2013 }, new string[] { "Red Bull Racing" }),
4242
new("Nico", "Rosberg", "Germany", 206, 24, new int[] { 2016 }, new string[] { "Mercedes" }),
4343
new("Max", "Verstappen", "Netherlands", 160, 33, new int[] { 2021, 2022 }, new string[] { "Red Bull Racing" }),
44-
};
44+
];
4545
}

2_Libs/Networking/HttpServerSample/GenerateHtml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private IEnumerable<string> GetRequestInfo(HttpRequest request)
5656

5757
private IEnumerable<string> GetHeaderInfo(IHeaderDictionary headers)
5858
{
59-
List<(string Key, string? Value)> values = new();
59+
List<(string Key, string? Value)> values = [];
6060
var keys = headers.Keys;
6161
foreach (var key in keys)
6262
{

0 commit comments

Comments
 (0)