Skip to content

Commit f033bfc

Browse files
Merge branch 'main' into seo/88631-arraylist-vs-list-benchmark
2 parents da25196 + 0fdbaeb commit f033bfc

387 files changed

Lines changed: 5736 additions & 2542 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/pr-build.yml‎

Lines changed: 255 additions & 199 deletions
Large diffs are not rendered by default.

‎README.md‎

Lines changed: 160 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,171 @@
1+
<div align="center">
2+
13
# Code Maze Guides
2-
Code Maze is a blog about simplicity and pragmatism. We tend to write clear and simple articles, easily understandable by everyone.
34

4-
We run a primarily C#/.NET (Core) blog, with some accent on web development and web applications. Besides trying to implement best practices and use the newest technologies available, we are focusing on the entire lifecycle of .NET (Core) applications.
5+
**The source code for 800+ C# and .NET articles from [code-maze.com](https://code-maze.com/?source=github).**<br>Every article gets its own runnable solution, most with tests.
6+
7+
[![Stars](https://img.shields.io/github/stars/CodeMazeBlog/CodeMazeGuides?style=flat&logo=github)](https://github.com/CodeMazeBlog/CodeMazeGuides/stargazers) [![Forks](https://img.shields.io/github/forks/CodeMazeBlog/CodeMazeGuides?style=flat&logo=github)](https://github.com/CodeMazeBlog/CodeMazeGuides/network/members) [![PR Build](https://github.com/CodeMazeBlog/CodeMazeGuides/actions/workflows/pr-build.yml/badge.svg)](https://github.com/CodeMazeBlog/CodeMazeGuides/actions/workflows/pr-build.yml) [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) [![.NET](https://img.shields.io/badge/.NET-512BD4?logo=dotnet&logoColor=white)](https://dotnet.microsoft.com/)
8+
9+
[Blog](https://code-maze.com/?source=github) · [Courses](https://courses.code-maze.com/courses/?source=github) · [YouTube](https://www.youtube.com/@CodeMaze) · [Report an issue](https://github.com/CodeMazeBlog/CodeMazeGuides/issues)
10+
11+
</div>
12+
13+
---
14+
15+
## Contents
16+
17+
- [About](#about)
18+
- [Quick start](#quick-start)
19+
- [Browse by topic](#browse-by-topic)
20+
- [Repository layout](#repository-layout)
21+
- [Contributing](#contributing)
22+
- [Support Code Maze](#support-code-maze)
23+
- [License](#license)
24+
25+
## About
26+
27+
Code Maze is a blog about C# and .NET, written with simplicity and pragmatism in mind. The articles are hands-on. They focus on the core of each topic and show working code, not theory alone.
28+
29+
This repository holds that code. If you came here from an article, the article links to its exact folder. If you are just browsing, pick a topic below.
30+
31+
## Quick start
32+
33+
You need the [.NET SDK](https://dotnet.microsoft.com/download). Each project states its target framework in its `.csproj` file, so install the version that project asks for.
34+
35+
```bash
36+
git clone https://github.com/CodeMazeBlog/CodeMazeGuides.git
37+
cd CodeMazeGuides/<topic>/<article-folder>
38+
dotnet build
39+
dotnet run
40+
dotnet test
41+
```
42+
43+
> [!TIP]
44+
> The full repository is large. To get only one article, use a sparse checkout:
45+
>
46+
> ```bash
47+
> git clone --filter=blob:none --sparse https://github.com/CodeMazeBlog/CodeMazeGuides.git
48+
> cd CodeMazeGuides
49+
> git sparse-checkout set <topic>/<article-folder>
50+
> ```
51+
52+
## Browse by topic
53+
54+
The numbers show how many article projects each folder holds.
55+
56+
### C# language
57+
58+
| Topic | Projects |
59+
| --- | ---: |
60+
| [C# basics](csharp-basic-topics) | 45 |
61+
| [C# intermediate](csharp-intermediate-topics) | 48 |
62+
| [C# advanced](csharp-advanced-topics) | 28 |
63+
| [Classes, structs and records](csharp-classes-struct-record) | 11 |
64+
| [Methods](csharp-methods) | 9 |
65+
| [Operators](csharp-operators) | 5 |
66+
| [Strings](strings-csharp) | 39 |
67+
| [Numbers](numbers-csharp) | 19 |
68+
| [LINQ](csharp-linq) | 20 |
69+
| [Async programming](async-csharp) | 11 |
70+
| [Threads](threads-csharp) | 7 |
71+
| [Exception handling](exception-handling) | 10 |
72+
| [Refactoring](csharp-refactoring) | 5 |
73+
74+
### Collections
75+
76+
| Topic | Projects |
77+
| --- | ---: |
78+
| [Collections](collections-csharp) | 24 |
79+
| [Arrays](collections-arrays) | 24 |
80+
| [Lists](collections-lists) | 16 |
81+
| [Dictionaries](collections-dictionary) | 9 |
82+
83+
### ASP.NET Core and Blazor
84+
85+
| Topic | Projects |
86+
| --- | ---: |
87+
| [ASP.NET Core Web API](aspnetcore-webapi) | 56 |
88+
| [ASP.NET Core features](aspnetcore-features) | 52 |
89+
| [Authentication and authorization](authorization-dotnet) | 23 |
90+
| [Query strings](dotnet-querystrings) | 6 |
91+
| [Blazor features](blazor-features) | 11 |
92+
| [Blazor libraries](blazor-libraries) | 2 |
93+
| [Angular](angular) | 1 |
94+
95+
### Data and serialization
96+
97+
| Topic | Projects |
98+
| --- | ---: |
99+
| [Entity Framework Core](dotnet-efcore) | 28 |
100+
| [Dapper](dotnet-dapper) | 4 |
101+
| [MongoDB](dotnet-mongo-db) | 3 |
102+
| [JSON](json-csharp) | 29 |
103+
| [XML](xml-csharp) | 6 |
104+
| [Files and streams](files-csharp) | 23 |
105+
| [Date and time](dotnet-datetime) | 22 |
106+
| [Searching](dotnet-searching) | 1 |
107+
108+
### Architecture and design
109+
110+
| Topic | Projects |
111+
| --- | ---: |
112+
| [Design patterns](csharp-design-patterns) | 12 |
113+
| [Architectural patterns](csharp-architectural-patterns) | 13 |
114+
| [Domain-driven design](domain-driven%20design) | 1 |
115+
| [Dependency injection](dotnet-dependency-injection) | 11 |
116+
| [DI tools](dependency-injection-tools) | 3 |
117+
| [Microservices](dotnet-microservices) | 2 |
118+
119+
### Libraries, tooling and operations
120+
121+
| Topic | Projects |
122+
| --- | ---: |
123+
| [Client libraries](dotnet-client-libraries) | 60 |
124+
| [Base libraries](dotnet-base-libraries) | 3 |
125+
| [Testing](dotnet-testing) | 32 |
126+
| [Logging](dotnet-logging) | 13 |
127+
| [Performance](dotnet-performance) | 9 |
128+
| [.NET CLI](dotnet-cli) | 2 |
129+
| [Visual Studio](visual-studio) | 5 |
130+
| [Deployment](dotnet-deployment) | 2 |
131+
| [Platform-specific code](dotnet-platform-specific) | 3 |
132+
| [Projects](dotnet-projects) | 13 |
133+
134+
### Algorithms, AI and more
135+
136+
| Topic | Projects |
137+
| --- | ---: |
138+
| [Algorithms](csharp-algorithms) | 13 |
139+
| [Machine learning](csharp-machine-learning) | 5 |
140+
| [AI](csharp-ai) | 1 |
141+
| [Images](csharp-images) | 3 |
142+
143+
## Repository layout
144+
145+
```text
146+
CodeMazeGuides/
147+
├── <topic>/ # one folder per topic, e.g. aspnetcore-webapi
148+
│ └── <article-folder>/ # one folder per article
149+
│ ├── <Project>.sln # its own solution
150+
│ ├── <Project>/ # the example code
151+
│ └── Tests/ # tests, in most articles
152+
└── .github/workflows/ # PR build: builds and tests changed projects
153+
```
5154
6-
We think that the combination of a hands-on approach and focusing on the essence of the topic in our articles is what makes our blog unique. Hopefully, this approach will be something that helps you learn quicker and achieve the best results possible.
155+
Each article is self-contained. You never need to build the whole repository.
7156

8-
This repository contains most of the code from our blog.
157+
## Contributing
9158

10-
The Materials
11-
==============================
12-
If you like what you've learned and would like to support us, you can check some of our materials:
159+
Found a bug in an example, or does a project not build on a newer .NET version? [Open an issue](https://github.com/CodeMazeBlog/CodeMazeGuides/issues) or send a pull request. The PR build checks every changed project automatically.
13160

14-
Here's the "Ultimate ASP.NET Core WEB API - Second Edition": <br/>
15-
**[ULTIMATE ASP.NET CORE WEB API](https://code-maze.com/ultimate-aspnetcore-webapi-second-edition/?source=github)**
161+
## Support Code Maze
16162

17-
Here's the "Blazor WebAssembly (.NET 7)" Video Course: <br/>
18-
**[BLAZOR WEBASSEMBLY VIDEO COURSE](https://code-maze.com/blazor-webassembly-course/?source=github)**
163+
The blog is free. Our courses keep it that way. If the articles helped you, have a look:
19164

20-
| Ultimate Web API | Blazor WebAssembly |
21-
| - | - |
22-
| [<img src="https://code-maze.com/wp-content/uploads/2022/08/ultimate-aspnet-core-banner-v3-690-min.webp" width="500">](https://code-maze.com/ultimate-aspnetcore-webapi-second-edition/?source=github) | [<img src="https://code-maze.com/wp-content/uploads/2021/04/banner-blog-final-min.png" width="500">](https://code-maze.com/blazor-webassembly-course/?source=github) |
165+
<a href="https://courses.code-maze.com/courses/?source=github"><strong>Browse all Code Maze courses →</strong></a>
23166

24-
We appreciate your support. It helps us provide even more free materials on the blog.
167+
A ⭐ on this repository also helps other developers find it.
25168

26-
Running the Examples
27-
================
28-
Each article is separated into its own folder on the repository and has its own solution. Whether you came from the article or just browsing the repository, each solution can be built separately with `dotnet build` or run with `dotnet run`.
169+
## License
29170

30-
On top of that, most of the solutions contain additional tests you can browse and run with `dotnet test`. Tests usually serve to verify the solution, but you can often find some useful ideas and additional examples.
171+
[MIT](LICENSE). Use the code freely in your own projects.

‎aspnetcore-features/HttpClientDefaultAndPerRequestTimeOut/HttpClientDefaultAndPerRequestTimeOut/HttpClientDefaultAndPerRequestTimeOut.csproj‎

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

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
5-
<Nullable>disable</Nullable>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<UserSecretsId>d0d8591a-b364-4f96-921f-d24a0e638079</UserSecretsId>
8-
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9-
<DockerfileContext>.</DockerfileContext>
108
</PropertyGroup>
119

1210
<ItemGroup>
13-
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
11+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="10.10.0" />
1412
</ItemGroup>
1513

1614
<ItemGroup>

‎aspnetcore-features/HttpClientDefaultAndPerRequestTimeOut/HttpClientDefaultAndPerRequestTimeOut/Program.cs‎

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
using Microsoft.AspNetCore.Builder;
2-
using Microsoft.AspNetCore.Mvc;
3-
using System.Net.Http;
4-
using System.Threading;
5-
61
var builder = WebApplication.CreateBuilder(args);
72

8-
// Add services to the container.
9-
10-
builder.Services.AddControllers();
11-
123
builder.Services.AddHttpClient("TestClient", (sp, httpClient) =>
134
{
145
var configuration = sp.GetRequiredService<IConfiguration>();
@@ -18,15 +9,17 @@
189
httpClient.Timeout = TimeSpan.FromSeconds(timeoutSeconds);
1910
});
2011

21-
var app = builder.Build();
22-
23-
// Configure the HTTP request pipeline.
24-
25-
app.UseHttpsRedirection();
26-
27-
app.UseAuthorization();
12+
builder.Services.AddHttpClient("ResilientClient", httpClient =>
13+
{
14+
httpClient.BaseAddress = new Uri("http://localhost:5000");
15+
})
16+
.AddStandardResilienceHandler(options =>
17+
{
18+
options.AttemptTimeout.Timeout = TimeSpan.FromSeconds(2);
19+
options.TotalRequestTimeout.Timeout = TimeSpan.FromSeconds(8);
20+
});
2821

29-
app.MapControllers();
22+
var app = builder.Build();
3023

3124
app.MapGet("/api/delay-4-seconds", async (CancellationToken cancellationToken) => await Task.Delay(TimeSpan.FromSeconds(4), cancellationToken));
3225

@@ -36,7 +29,7 @@
3629

3730
try
3831
{
39-
var response = await httpClient.GetAsync("/api/delay-4-seconds");
32+
using var response = await httpClient.GetAsync("/api/delay-4-seconds");
4033

4134
return Results.Ok();
4235
}
@@ -53,7 +46,7 @@
5346

5447
try
5548
{
56-
var response = await httpClient.GetAsync($"/api/delay-4-seconds", cancellationToken);
49+
using var response = await httpClient.GetAsync($"/api/delay-4-seconds", cancellationToken);
5750

5851
return Results.Ok();
5952
}
@@ -71,7 +64,7 @@
7164

7265
try
7366
{
74-
var response = await httpClient.GetAsync("/api/delay-4-seconds", tokenSource.Token);
67+
using var response = await httpClient.GetAsync("/api/delay-4-seconds", tokenSource.Token);
7568

7669
return Results.Ok();
7770
}
@@ -83,4 +76,40 @@
8376
}
8477
});
8578

79+
app.MapGet("/api/test-timeout-vs-cancellation", async (IHttpClientFactory httpClientFactory, CancellationToken cancellationToken) =>
80+
{
81+
var httpClient = httpClientFactory.CreateClient("TestClient");
82+
83+
try
84+
{
85+
using var response = await httpClient.GetAsync("/api/delay-4-seconds", cancellationToken);
86+
87+
return Results.Ok();
88+
}
89+
catch (OperationCanceledException ex) when (ex.InnerException is TimeoutException)
90+
{
91+
return Results.Text("The request timed out");
92+
}
93+
catch (OperationCanceledException)
94+
{
95+
return Results.Text("The caller canceled the request");
96+
}
97+
});
98+
99+
app.MapGet("/api/test-resilience-timeout", async (IHttpClientFactory httpClientFactory) =>
100+
{
101+
var httpClient = httpClientFactory.CreateClient("ResilientClient");
102+
103+
try
104+
{
105+
using var response = await httpClient.GetAsync("/api/delay-4-seconds");
106+
107+
return Results.Ok();
108+
}
109+
catch (Exception ex)
110+
{
111+
return Results.Text($"{ex.GetType().Name} after {httpClient.Timeout}");
112+
}
113+
});
114+
86115
app.Run();

‎aspnetcore-features/HttpClientDefaultAndPerRequestTimeOut/Tests/HttpClientDefaultAndPerRequestTimeOut.Tests/EndpointsLiveTests.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,30 @@ public async Task GivenCustomTokenIsCombinedWithGlobalAndRequestToken_WhenTheReq
5555
// Assert
5656
await Assert.ThrowsAsync<TaskCanceledException>(() => client.GetAsync("/api/test-combined-timeout", cts.Token));
5757
}
58+
59+
[Fact]
60+
public async Task WhenTheTimeoutElapses_ThenTheOperationCanceledExceptionNestsATimeoutException()
61+
{
62+
// Arrange
63+
var client = _factory.CreateClient();
64+
65+
// Act
66+
var responseMessage = await client.GetStringAsync("/api/test-timeout-vs-cancellation");
67+
68+
// Assert
69+
Assert.Equal("The request timed out", responseMessage);
70+
}
71+
72+
[Fact]
73+
public async Task WhenTheResilienceHandlerTimesOut_ThenItThrowsTimeoutRejectedExceptionAndDisablesHttpClientTimeout()
74+
{
75+
// Arrange
76+
var client = _factory.CreateClient();
77+
78+
// Act
79+
var responseMessage = await client.GetStringAsync("/api/test-resilience-timeout");
80+
81+
// Assert
82+
Assert.Equal("TimeoutRejectedException after -00:00:00.0010000", responseMessage);
83+
}
5884
}

‎aspnetcore-features/HttpClientDefaultAndPerRequestTimeOut/Tests/HttpClientDefaultAndPerRequestTimeOut.Tests/HttpClientDefaultAndPerRequestTimeOut.Tests.csproj‎

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

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

@@ -10,11 +10,11 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="coverlet.collector" Version="6.0.0" />
14-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.4" />
15-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
16-
<PackageReference Include="xunit" Version="2.5.3" />
17-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
13+
<PackageReference Include="coverlet.collector" Version="10.0.1" />
14+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.12" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
16+
<PackageReference Include="xunit" Version="2.9.3" />
17+
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)