Skip to content

Route handler filters don't handle all return types #41213

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
BrennanConroy opened this issue Apr 15, 2022 · 2 comments
Closed

Route handler filters don't handle all return types #41213

BrennanConroy opened this issue Apr 15, 2022 · 2 comments
Assignees
Labels
bug This issue describes a behavior which is not expected - a bug. old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Milestone

Comments

@BrennanConroy
Copy link
Member

Filters currently wrap the user delegate in a ValueTask<object?> before handing off the function to the normal route handling logic. This causes issues with any non object returning methods like void, Task, Task<T>, etc. returning methods.

Void returning method with no-op filter:

void Method() { }
app.MapGet("/method", Method).AddFilter((c, n) => n(c));

Output:

ArgumentException: Expression of type 'System.Void' cannot be used for parameter of type 'System.Object' of method 'System.Threading.Tasks.ValueTask`1[System.Object] WrapObjectAsValueTask(System.Object)' (Parameter 'arg0')

Task returning method with no-op filter:

Task Method() => Task.CompletedTask;
app.MapGet("/method", Method).AddFilter((c, n) => n(c));

Output:

{"result":{},"id":54,"exception":null,"status":5,"isCanceled":false,"isCompleted":true,"isCompletedSuccessfully":true,"creationOptions":0,"asyncState":null,"isFaulted":false}

@BrennanConroy BrennanConroy added bug This issue describes a behavior which is not expected - a bug. old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Apr 15, 2022
@lohithgn
Copy link

@BrennanConroy @danroth27
I cant event compile the code snippet provided in Preview 3 blog post:

app.MapGet("/hello/{name}", (string name) => $"Hello, {name}!")
    .AddFilter((context, next) =>
    {
        var name = (string) context.Parameters[0];
        if (name == "Bob")
        {
            return Result.Problem("No Bob's allowed");
        }
        return next(context);
    });

It throws up error: cannot convert type IResult to ValueTask<Object?>
image

I found @captainsafia example here: https://github.com/captainsafia/MinimalFiltersExample/blob/main/Program.cs

Code compiles but i have a run time exception for next delegate:
image

Stack trace:

System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method2(Closure, RouteHandlerInvocationContext)
   at Program.<>c__DisplayClass0_0.<<<Main>$>b__3>d.MoveNext() in C:\w\aspnet\WebApplication1\Program.cs:line 13
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteObjectReturn(Object obj, HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

How do we get this to work ?

@BrennanConroy
Copy link
Member Author

The null ref is issue #41188, you can workaround it by using a method instead of an inline delegate.

@captainsafia captainsafia self-assigned this Apr 19, 2022
@ghost ghost locked as resolved and limited conversation to collaborators May 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue describes a behavior which is not expected - a bug. old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

No branches or pull requests

3 participants