Simple logic to forward cancellation token and http context

This commit is contained in:
2024-09-25 22:13:59 +02:00
parent f04f3eafbd
commit 45327d1618
2 changed files with 9 additions and 3 deletions
@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<Version>0.8.13</Version> <Version>0.8.14</Version>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Alexandru Macocian</Authors> <Authors>Alexandru Macocian</Authors>
@@ -221,14 +221,19 @@ public class UseRoutesGenerator : IIncrementalGenerator
.Append(@$".AddEndpointFilter<{routeFilterType}>()"); .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 parameters = GetMethodParameters(methodDeclarationSyntax);
var variables = string.Join(", ", methodDeclarationSyntax.ParameterList.Parameters var variables = string.Join(", ", methodDeclarationSyntax.ParameterList.Parameters
.Select(param => param.Identifier.Text)); .Select(param => param.Identifier.Text));
return @$" 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}); return await route.{methodDeclarationSyntax.Identifier}({variables});
}}){routeFilterSb};"; }}){routeFilterSb};";
} }
@@ -236,6 +241,7 @@ public class UseRoutesGenerator : IIncrementalGenerator
private static string GetMethodParameters(MethodDeclarationSyntax methodDeclaration) private static string GetMethodParameters(MethodDeclarationSyntax methodDeclaration)
{ {
var parameters = methodDeclaration.ParameterList.Parameters var parameters = methodDeclaration.ParameterList.Parameters
.Where(param => param.Identifier.Text is not "httpContext" and not "cancellationToken")
.Select(param => .Select(param =>
{ {
var parameterType = param.Type?.ToString(); var parameterType = param.Type?.ToString();