Skip to content

Commit ae6f5df

Browse files
committed
Add chained sample for IExceptionHandler
1 parent ce01763 commit ae6f5df

File tree

6 files changed

+119
-3
lines changed

6 files changed

+119
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Samples for ASP.NET Core 6.0, 7.0 and 8.0 RC 1 (501)
1+
# Samples for ASP.NET Core 6.0, 7.0 and 8.0 RC 1 (502)
22

3-
- Samples for ASP.NET Core **8.0 RC 1** is available [here](/projects/.net8) (38).
3+
- Samples for ASP.NET Core **8.0 RC 1** is available [here](/projects/.net8) (39).
44
- Samples for ASP.NET Core **7.0** is available [here](/projects/.net7) (45).
55
- Samples for ASP.NET Core **8.0 Preview 6** using EdgeDB.NET is [here](https://github.com/edgedb/edgedb-net).
66

projects/.net8/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ASP.NET 8.0 Preview 7 (38)
1+
# ASP.NET 8.0 Preview 7 (39)
22

33
These samples require [.NET 8.0 RC 1](https://github.com/dotnet/installer#table).
44

@@ -159,6 +159,10 @@ These samples require [.NET 8.0 RC 1](https://github.com/dotnet/installer#table)
159159

160160
Implement `IExceptionHandler` to handle ASP.NET Core exceptions.
161161

162+
* [Multiple IExceptionHandler](iexception-handler-2)
163+
164+
Implement multiple `IExceptionHandler` to handle ASP.NET Core exceptions.
165+
162166
## Keyed Services
163167

164168
* [Keyed Services in Minimal API](keyed-service)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Microsoft.AspNetCore.Diagnostics;
2+
3+
var builder = WebApplication.CreateBuilder();
4+
builder.Services.AddExceptionHandler<TimeOutHandler>();
5+
builder.Services.AddExceptionHandler<DefaultExceptionHandler>();
6+
7+
var app = builder.Build();
8+
app.UseExceptionHandler(opt => { });
9+
10+
app.MapGet("/", () =>
11+
{
12+
return Results.Content("""
13+
<html>
14+
<body>
15+
<ul>
16+
<li><a href="other-exception">Other exception</a></li>
17+
<li><a href="time-out">Time out</a></li>
18+
</ul>
19+
</body
20+
</html>
21+
""", "text/html");
22+
});
23+
24+
app.MapGet("/other-exception", () =>
25+
{
26+
throw new Exception("Something went wrong");
27+
});
28+
29+
app.MapGet("/time-out", () =>
30+
{
31+
throw new TimeoutException("Out of time");
32+
});
33+
34+
app.Run();
35+
36+
public class TimeOutHandler : IExceptionHandler
37+
{
38+
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
39+
{
40+
if (exception is TimeoutException)
41+
{
42+
await httpContext.Response.WriteAsync($"""
43+
<html>
44+
<body>
45+
From {nameof(TimeOutHandler)}. The exception message is {exception.Message} at {httpContext.Request.Method} {httpContext.Request.Path}.
46+
</body>
47+
</html>
48+
""");
49+
return true;
50+
}
51+
52+
return false;
53+
}
54+
}
55+
56+
public class DefaultExceptionHandler : IExceptionHandler
57+
{
58+
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
59+
{
60+
await httpContext.Response.WriteAsync($"""
61+
<html>
62+
<body>
63+
From {nameof(DefaultExceptionHandler)}. The exception message is {exception.Message} at {httpContext.Request.Method} {httpContext.Request.Path}.
64+
</body>
65+
</html>
66+
""");
67+
return true;
68+
}
69+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Implement multiple IExceptionHandler to handle unhandled exceptions
2+
3+
Implement multiple `IExceptionHandler` for handling exceptions.
4+
5+
```csharp
6+
builder.Services.AddExceptionHandler<TimeOutHandler>();
7+
builder.Services.AddExceptionHandler<DefaultExceptionHandler>();
8+
```
9+
10+
In this case **the order** of the handles are **important**. `TimeOutHandler` is called first then `DefaultExceptionHandler`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<ImplicitUsings>true</ImplicitUsings>
5+
<LangVersion>preview</LangVersion>
6+
<PreviewFeatures>true</PreviewFeatures>
7+
</PropertyGroup>
8+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "iexception-handler-2", "iexception-handler-2.csproj", "{01EC414C-2BCA-4A70-905B-F38AA550B4BC}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{01EC414C-2BCA-4A70-905B-F38AA550B4BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{01EC414C-2BCA-4A70-905B-F38AA550B4BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{01EC414C-2BCA-4A70-905B-F38AA550B4BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{01EC414C-2BCA-4A70-905B-F38AA550B4BC}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {295FBCEC-01B4-4876-8090-EB36054B692A}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)