-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathHandleCancellationMiddleware.cs
48 lines (42 loc) · 1.68 KB
/
HandleCancellationMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.Extensions;
using Microsoft.Extensions.Logging;
namespace Microsoft.Azure.WebJobs.Script.WebHost.Middleware
{
internal class HandleCancellationMiddleware
{
private readonly ILogger _logger;
private readonly RequestDelegate _next;
public HandleCancellationMiddleware(RequestDelegate next, ILogger<HandleCancellationMiddleware> logger)
{
_logger = logger;
_next = next;
}
public async Task Invoke(HttpContext context)
{
var requestId = context.Request.HttpContext.Items[ScriptConstants.AzureFunctionsRequestIdKey].ToString();
try
{
await _next.Invoke(context);
if (context.RequestAborted.IsCancellationRequested && !context.Response.HasStarted)
{
_logger.RequestAborted(requestId);
context.Response.StatusCode = StatusCodes.Status499ClientClosedRequest;
}
}
catch (Exception ex) when ((ex is OperationCanceledException || ex is IOException) && context.RequestAborted.IsCancellationRequested)
{
_logger.RequestAborted(requestId);
if (!context.Response.HasStarted)
{
context.Response.StatusCode = StatusCodes.Status499ClientClosedRequest;
}
}
}
}
}