Skip to content

Commit ce01763

Browse files
committed
Add using default policy in MVC controller
1 parent 90c0b46 commit ce01763

File tree

8 files changed

+104
-4
lines changed

8 files changed

+104
-4
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 (500)
1+
# Samples for ASP.NET Core 6.0, 7.0 and 8.0 RC 1 (501)
22

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

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

@@ -148,6 +148,10 @@ These samples require [.NET 8.0 RC 1](https://github.com/dotnet/installer#table)
148148
* [Request Timeout](request-timeout-5)
149149

150150
Use `RequestTimeout` attribute in an MVC controller to use a named policy.
151+
152+
* [Request Timeout](request-timeout-6)
153+
154+
Use default timeout policy in an MVC controller.
151155

152156
## IExceptionHandler
153157

projects/.net8/request-timeout-3/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ hello world
2828
</body>
2929
</html>
3030
""", "text/html");
31-
}).WithRequestTimeout(policy: null);
31+
});
3232

3333
app.Run();
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-3", "request-timeout-3.csproj", "{3A11F546-ED08-406A-937F-7F936C3576C4}"
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+
{3A11F546-ED08-406A-937F-7F936C3576C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3A11F546-ED08-406A-937F-7F936C3576C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3A11F546-ED08-406A-937F-7F936C3576C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3A11F546-ED08-406A-937F-7F936C3576C4}.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 = {B450A42C-0D5C-470F-9C2D-63D98D0102CD}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.DefaultPolicy = 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+
public async Task<IActionResult> Index()
30+
{
31+
await Task.Delay(100);
32+
HttpContext.RequestAborted.ThrowIfCancellationRequested();
33+
return Ok("Hello World!");
34+
}
35+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rely on default timeout policy on MVC
2+
3+
This sample shows on how to use default timeout policy on MVC. There is no need to specify `RequestTimeout` attribute.
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-6", "request-timeout-6.csproj", "{9197264F-FB0C-4498-BB16-8EAC6B5D82BA}"
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+
{9197264F-FB0C-4498-BB16-8EAC6B5D82BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9197264F-FB0C-4498-BB16-8EAC6B5D82BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9197264F-FB0C-4498-BB16-8EAC6B5D82BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9197264F-FB0C-4498-BB16-8EAC6B5D82BA}.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 = {C27BA2BF-56AA-42AF-B03E-59ECFB01A627}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)