Support Custom Awaitable Types in Minimal API's
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 276
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
Currently custom awaitables are [not supported and it is marked as a `TODO` in the code](https://github.com/dotnet/aspnetcore/blob/main/src/Http/Http.Extensions/src/RequestDelegateFactory.cs#L643-L644). Honestly, I don't have a use for this myself, I stick with `Task` and `ValueTask` but they exist out there and it would be nice to support them.
### Describe the solution you'd like
Since a lot of [work is done on this line](https://github.com/dotnet/aspnetcore/blob/main/src/Http/Http.Extensions/src/RequestDelegateFactory.cs#L573) to test whether the type is awaitable but the `AwaitableInfo` that method can return is discarded. I propose that information is used to create a custom `Awaitable` that also implements everything required to be an awaitable [very similarly to `ObjectMethodExecutorAwaitable`](https://github.com/dotnet/aspnetcore/blob/main/src/Shared/ObjectMethodExecutor/ObjectMethodExecutorAwaitable.cs). I believe there would be two versions of this. One with a void result and one non-void result. We can also utilize generics to create this object to minimize boxing. That awaitable is created using `Expression.New` and sent into a `Execute[...]` method so that we can `await` it.
One thing I propose this intentionally doesn't support is is the input delegate has a `returnType` of `System.Object`. Currently when objects have that return type it gets sent to [this method](https://github.com/dotnet/aspnetcore/blob/main/src/Http/Http.Extensions/src/RequestDelegateFactory.cs#L1496-L1544). To support custom awaitables in that method we would have to utilize a lot of reflection each and every call and that would most likely slow down all the other `Task<>` and such calls so that it would be fine not supporting this for calls that have to go through the runtime to determine how to format their output.
### Additional context
I have begun [working on this](https://github.com/justindbaur/aspnetcore/blob/custom-await/src/Http/Http.Extensions/src/RequestDelegateFactory.cs#L650-L737) but PR's require an issue to exist. I'm not quite ready to create a PR because I need to make sure it's fully tested but I am close.
Contributor guide
Assessment
This issue has not been assessed yet.