Skip to content

Commit 42e3e6b

Browse files
committed
Add request timeout example
1 parent 17e33e1 commit 42e3e6b

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
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 (469)
1+
# Samples for ASP.NET Core 6.0 (470)
22

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

55
Samples for ASP.NET Core **7.0** is available [here](/projects/.net7) (45).
66

projects/.net8/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ASP.NET 8.0 Preview 3 (7)
1+
# ASP.NET 8.0 Preview 3 (8)
22

33
These samples require [.NET 8.0 Preview 3](https://dotnet.microsoft.com/en-us/download/dotnet/8.0).
44

@@ -29,4 +29,8 @@ These samples require [.NET 8.0 Preview 3](https://dotnet.microsoft.com/en-us/do
2929

3030
* [RazorComponentSix](RazorComponentSix)
3131

32-
This sample demonstrates the new section functionality using `SectionOutlet` to mark a section and `SectionContent` to supply the content to a section.
32+
This sample demonstrates the new section functionality using `SectionOutlet` to mark a section and `SectionContent` to supply the content to a section.
33+
34+
* [Request Timeout](request-timeout)
35+
36+
This sample demonstrates how to configure a request timeout.

projects/.net8/build.bat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ dotnet build RazorComponentFour
44
dotnet build RazorComponentOne
55
dotnet build RazorComponentSix
66
dotnet build RazorComponentThree
7-
dotnet build RazorComponentTwo
7+
dotnet build RazorComponentTwo
8+
dotnet build request-timeout
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var builder = WebApplication.CreateBuilder();
2+
builder.Services.AddRequestTimeouts();
3+
4+
var app = builder.Build();
5+
app.UseRequestTimeouts();
6+
7+
app.MapGet("/", async (HttpContext context) => {
8+
await Task.Delay(TimeSpan.FromSeconds(2));
9+
10+
if (context.RequestAborted.IsCancellationRequested)
11+
return Results.Content("timeout", "text/plain");
12+
13+
return Results.Content("""
14+
<html>
15+
<body>
16+
hello world
17+
</body>
18+
</html>
19+
""", "text/html");
20+
}).WithRequestTimeout(TimeSpan.FromSeconds(1));
21+
22+
app.Run();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Enable timeout
2+
3+
Use `AddRequestTimeouts` to enable timeout functionality in your endpoints. Check `HttpContext.RequestAborted.IsCancellationRequested` to see if the request has timed out.
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)