Skip to content

Commit 788fcb8

Browse files
committed
Add a named timeout policy example
1 parent c5b5365 commit 788fcb8

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-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 (471)
1+
# Samples for ASP.NET Core 6.0 (472)
22

3-
Samples for ASP.NET Core **8.0 Preview 4** is available [here](/projects/.net8) (9).
3+
Samples for ASP.NET Core **8.0 Preview 4** is available [here](/projects/.net8) (10).
44

55
Samples for ASP.NET Core **7.0** is available [here](/projects/.net7) (45).
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 4 (9)
1+
# ASP.NET 8.0 Preview 4 (10)
22

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

@@ -35,6 +35,10 @@ These samples require [.NET 8.0 Preview 4](https://github.com/dotnet/installer#t
3535

3636
This sample demonstrates how to configure a request timeout.
3737

38+
* [Request Timeout Policy](request-timeout-2)
39+
40+
Trigger exception on a timeout using `HttpContext.RequestAborted.ThrowIfCancellationRequested()` on a timeout that was specified using a named policy.
41+
3842
* [Short Circuit](map-short-circuit)
3943

4044
Use `MapShortCircuit` or `.ShortCircuit()` to efficiently respond to a request without going through a middleware pipeline run.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var builder = WebApplication.CreateBuilder();
2+
builder.Services.AddRequestTimeouts(options =>
3+
{
4+
options.AddPolicy("quick", new Microsoft.AspNetCore.Http.Timeouts.RequestTimeoutPolicy
5+
{
6+
Timeout = TimeSpan.FromSeconds(1),
7+
TimeoutStatusCode = 200,
8+
WriteTimeoutResponse = async (HttpContext context) =>
9+
{
10+
context.Response.ContentType = "text/plain";
11+
await context.Response.WriteAsync("timeout is triggered");
12+
}
13+
});
14+
});
15+
16+
var app = builder.Build();
17+
app.UseRequestTimeouts();
18+
19+
app.MapGet("/", async (HttpContext context) => {
20+
await Task.Delay(TimeSpan.FromSeconds(2));
21+
22+
context.RequestAborted.ThrowIfCancellationRequested();
23+
24+
return Results.Content("""
25+
<html>
26+
<body>
27+
hello world
28+
</body>
29+
</html>
30+
""", "text/html");
31+
}).WithRequestTimeout("quick");
32+
33+
app.Run();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Name timeout policy
2+
3+
Trigger exception on a timeout using `HttpContext.RequestAborted.ThrowIfCancellationRequested()` on a timeout that was specified using a named policy.
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>

0 commit comments

Comments
 (0)