Open bacongobbler opened 3 months ago
Yeah, I ran into this yesterday. After some experimentation, I found that this worked, at least:
app.MapGet(
"/hellosql",
async context =>
{
await HelloSql();
}
);
// ...
private static async Task<string> HelloSql()
{
// ...
}
That was inspired by https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-8.0#endpoint-defined-outside-of-programcs, along with https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.endpointroutebuilderextensions.mapget?view=aspnetcore-8.0#microsoft-aspnetcore-builder-endpointroutebuilderextensions-mapget(microsoft-aspnetcore-routing-iendpointroutebuilder-system-string-microsoft-aspnetcore-http-requestdelegate) and https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.requestdelegate?view=aspnetcore-8.0.
The Map*
functions are heavily overloaded, and I'm guessing sometimes they need type hints in order to make sure you're using the right one, at least when NativeAOT is in the mix.
That does appear to work. Thank you!
Here's the updated sample:
app.MapPost("/", async context =>
{
var myService = context.RequestServices.GetRequiredService<IMyService>();
app.Logger.LogInformation("doing something");
await myService.DoSomething();
app.Logger.LogInformation("completed something");
context.Response.StatusCode = StatusCodes.Status204NoContent;
});
I'll keep this open for now with a note to document this for others.
Given a minimal API endpoint such as the following:
Results in an error at runtime:
I am assuming that the async result from the request handler needs to be handled by WasiHttpServer before passing it along to the resolver.