I try to understand solution of this channel CancellationTokenSource callback is called for all request In dotnet 5
I have the code bellow
public class AppTimeout
{
...
public AppTimeout(RequestDelegate next, IOptions<ServerOptions> options)
{
...
}
public async Task Invoke(HttpContext httpContext)
{
var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(httpContext.RequestAborted);
cancellationTokenSource.CancelAfter(_timeout);
httpContext.RequestAborted = cancellationTokenSource.Token;
var requestPath = httpContext.Request.Path;
cancellationTokenSource.Token.Register(() =>
{
logger.Message("... requestPath...");
});
await _next(httpContext);
}
}
After on request have been in timeout, The callback will be triggers for all request
But it is not case and the callback will be called only for the request in timeout just if I replace
cancellationTokenSource.Token.Register(() =>
by
using var registration = cancellationTokenSource.Token.Register(() =>
My question is why ? Could you explain me please?