From 45327d1618166a49d1f59b034d3bb778d3415f82 Mon Sep 17 00:00:00 2001 From: Alex Macocian Date: Wed, 25 Sep 2024 22:13:59 +0200 Subject: [PATCH] Simple logic to forward cancellation token and http context --- .../Net.Sdk.Web.Extensions.SourceGenerators.csproj | 2 +- .../UseRoutesGenerator.cs | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Net.Sdk.Web.Extensions.SourceGenerators/Net.Sdk.Web.Extensions.SourceGenerators.csproj b/Net.Sdk.Web.Extensions.SourceGenerators/Net.Sdk.Web.Extensions.SourceGenerators.csproj index d4ef6f2..b24e09a 100644 --- a/Net.Sdk.Web.Extensions.SourceGenerators/Net.Sdk.Web.Extensions.SourceGenerators.csproj +++ b/Net.Sdk.Web.Extensions.SourceGenerators/Net.Sdk.Web.Extensions.SourceGenerators.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 0.8.13 + 0.8.14 latest true Alexandru Macocian diff --git a/Net.Sdk.Web.Extensions.SourceGenerators/UseRoutesGenerator.cs b/Net.Sdk.Web.Extensions.SourceGenerators/UseRoutesGenerator.cs index 2e786d9..fe13ab8 100644 --- a/Net.Sdk.Web.Extensions.SourceGenerators/UseRoutesGenerator.cs +++ b/Net.Sdk.Web.Extensions.SourceGenerators/UseRoutesGenerator.cs @@ -221,14 +221,19 @@ public class UseRoutesGenerator : IIncrementalGenerator .Append(@$".AddEndpointFilter<{routeFilterType}>()"); } - // Get the method signature and generate it in Map + /* + * Get the parameters of the route method. Ignore CancellationToken cancellationToken and HttpContext httpContext. + * Those 2 will be automatically created by the generator. + * Simply let the user request them in the method signature and they should flow downstream. + */ var parameters = GetMethodParameters(methodDeclarationSyntax); var variables = string.Join(", ", methodDeclarationSyntax.ParameterList.Parameters .Select(param => param.Identifier.Text)); return @$" - builder.Map{type}(""{pattern}"", async ({classDeclarationSyntax.Identifier} route{(parameters.Length > 0 ? $", {parameters}" : "")}) => + builder.Map{type}(""{pattern}"", async (HttpContext httpContext, {classDeclarationSyntax.Identifier} route{(parameters.Length > 0 ? $", {parameters}" : "")}) => {{ + var cancellationToken = httpContext.RequestAborted; return await route.{methodDeclarationSyntax.Identifier}({variables}); }}){routeFilterSb};"; } @@ -236,6 +241,7 @@ public class UseRoutesGenerator : IIncrementalGenerator private static string GetMethodParameters(MethodDeclarationSyntax methodDeclaration) { var parameters = methodDeclaration.ParameterList.Parameters + .Where(param => param.Identifier.Text is not "httpContext" and not "cancellationToken") .Select(param => { var parameterType = param.Type?.ToString();