Skip to content

Commit 25bb13c

Browse files
committed
Add sample for RequestTimeout attribute using a policy
1 parent c95119e commit 25bb13c

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Use RequestTimeout attribute
1+
# Use RequestTimeout attribute in MVC
22

33
Use `RequestTimeout` attribute to specify how many milliseconds a time out will be trigerred.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.AspNetCore.Http.Timeouts;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
var builder = WebApplication.CreateBuilder();
5+
builder.Services.AddControllers();
6+
builder.Services.AddRequestTimeouts(options =>
7+
{
8+
options.AddPolicy("quick",new Microsoft.AspNetCore.Http.Timeouts.RequestTimeoutPolicy
9+
{
10+
Timeout = TimeSpan.FromMilliseconds(1),
11+
TimeoutStatusCode = 200,
12+
WriteTimeoutResponse = async (HttpContext context) =>
13+
{
14+
context.Response.ContentType = "text/plain";
15+
await context.Response.WriteAsync("timeout is triggered");
16+
}
17+
});
18+
});
19+
20+
var app = builder.Build();
21+
app.UseRequestTimeouts();
22+
app.MapControllers();
23+
24+
app.Run();
25+
26+
public class HomeController : ControllerBase
27+
{
28+
[HttpGet("/")]
29+
[RequestTimeout("quick")]
30+
public async Task<IActionResult> Index()
31+
{
32+
await Task.Delay(100);
33+
HttpContext.RequestAborted.ThrowIfCancellationRequested();
34+
return Ok("Hello World!");
35+
}
36+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Use RequestTimeout attribute in MVC
2+
3+
Use `RequestTimeout` attribute to specify which timeout policy to use.
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}") = "request-timeout-5", "request-timeout-5.csproj", "{79DB68DC-DFD0-4377-8092-51CB6132F18C}"
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+
{79DB68DC-DFD0-4377-8092-51CB6132F18C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{79DB68DC-DFD0-4377-8092-51CB6132F18C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{79DB68DC-DFD0-4377-8092-51CB6132F18C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{79DB68DC-DFD0-4377-8092-51CB6132F18C}.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 = {6A9E03E9-E1BE-4884-A2D2-AFDF8A2F4AA5}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)