Skip to content

Commit 7eedd8c

Browse files
committed
Add sample to default policy
1 parent 788fcb8 commit 7eedd8c

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

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

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

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

4040
Trigger exception on a timeout using `HttpContext.RequestAborted.ThrowIfCancellationRequested()` on a timeout that was specified using a named policy.
4141

42+
* [Request Timeout Policy](request-timeout-3)
43+
44+
Trigger exception on a timeout using `HttpContext.RequestAborted.ThrowIfCancellationRequested()` on a default timeout policy.
45+
4246
* [Short Circuit](map-short-circuit)
4347

4448
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.DefaultPolicy = 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(policy: null);
32+
33+
app.Run();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Default timeout policy
2+
3+
Trigger exception on a timeout using `HttpContext.RequestAborted.ThrowIfCancellationRequested()` on a default timeout 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)