Skip to content

Using filter (IRouteHandlerFilter) in minimal API throws exception #41464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
JohnyL opened this issue May 1, 2022 · 3 comments
Closed
1 task done

Using filter (IRouteHandlerFilter) in minimal API throws exception #41464

JohnyL opened this issue May 1, 2022 · 3 comments
Labels
✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved

Comments

@JohnyL
Copy link

JohnyL commented May 1, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Say, I have this simple API:

app.MapPost("/clients", (SearchCriteria search) => Results.Ok())
   .AddFilter<ValidationFilter<SearchCriteria>>();

I use empty validation filter:

public class ValidationFilter<T> : IRouteHandlerFilter where T : class
{
  public async ValueTask<object> InvokeAsync(
    RouteHandlerInvocationContext context,
    RouteHandlerFilterDelegate next)
  {
    return await next(context);
  }
}

SearchCriteria class:

public class SearchCriteria
{
  public int Id { get; set; }
  public string Name { get; set; }
}

Request to this API throws exception:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method5(Closure, RouteHandlerInvocationContext)
at ValidationFilter`1.InvokeAsync(RouteHandlerInvocationContext context, RouteHandlerFilterDelegate next) in E:\Projects\TestWebApi\TestWebApi\Program.cs:line 93
at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteObjectReturn(Object obj, HttpContext httpContext)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass64_2.<b__2>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

7.0.100-preview.3.22179.4

Anything else?

No response

@BrennanConroy
Copy link
Member

Dupe of #41188, fixed in preview4.

Workaround is to move the inline delegate into a function.

@BrennanConroy BrennanConroy added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label May 1, 2022
@ghost ghost added the Status: Resolved label May 1, 2022
@JohnyL
Copy link
Author

JohnyL commented May 1, 2022

@BrennanConroy That works. Thanks! 🙂

@ghost
Copy link

ghost commented May 2, 2022

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed May 2, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jun 1, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

No branches or pull requests

2 participants