Skip to content
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

Minimal APIs endpoints require explicitly converting Async<'T> to Task<'T> #1

Open
brianrourkeboll opened this issue Feb 26, 2023 · 0 comments

Comments

@brianrourkeboll
Copy link
Owner

brianrourkeboll commented Feb 26, 2023

Code like the following currently requires an explicit call to Async.StartAsTask in order for the asynchronous operation to be awaited by the framework, and for the correct response type metadata to be inferred:

get "/api/clowns/{id}" [
Status200OK, typeof<Dtos.Get.Clown>
Status404NotFound, null
Status500InternalServerError, typeof<ProblemDetails>
] (fun (logger : ILogger<Program>) (db : IDataAccess) id ->
db.Get id
|> AsyncResult.teeError (function
| Get.One.NotFound -> ()
| Get.One.DbExn e -> logger.LogError ("A database exception occurred when trying to get the clown.", e)
| Get.One.DbTimeout e -> logger.LogWarning ("A database timeout occurred when trying to get the clown.", e))
|> AsyncResult.foldResult
(Get.Clown.toDto >> Results.Ok)
(function
| Get.One.NotFound -> Results.NotFound ()
| Get.One.DbExn _ -> Results.Problem "A database exception occurred."
| Get.One.DbTimeout _ -> Results.Problem "A database timeout occurred.")
|> Async.StartAsTask)

In contrast, the equivalent code in a controller action method does not require the explicit conversion:

[<HttpGet("{id}")>]
[<ProducesResponseType(typeof<Dtos.Get.Clown>, Status200OK)>]
[<ProducesResponseType(Status404NotFound)>]
[<ProducesResponseType(typeof<ProblemDetails>, Status500InternalServerError)>]
member _.GetClown id =
dataAccess.Get id
|> AsyncResult.teeError (function
| Get.One.NotFound -> ()
| Get.One.DbExn e -> logger.LogError ("A database exception occurred when trying to get the clown.", e)
| Get.One.DbTimeout e -> logger.LogWarning ("A database timeout occurred when trying to get the clown.", e))
|> AsyncResult.foldResult
(Get.Clown.toDto >> Results.Ok)
(function
| Get.One.NotFound -> Results.NotFound ()
| Get.One.DbExn _ -> Results.Problem "A database exception occurred."
| Get.One.DbTimeout _ -> Results.Problem "A database timeout occurred.")

dotnet/aspnetcore#46898 will address this, if it is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant