1

I am using ASP.NET Core Minimal APIs with .NET 8.
I have an application that adds some endpoints. One of these endpoints is not valid, but the app starts normaly:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5031
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();


app.MapGet("/fine/{x}", (int x) => "I am {x}");

// Invalid, cause binding to the type 'NotBindable' of y is not possible
app.MapGet("/bad/{y}", (NotBindable y) => $"I am {y.Value}"); 

app.Run();

public class NotBindable
{
    public string Value { get; set; }
} 

When I request any of the endpoints, whether a valid one or an incorrect one, I get an exception (as expected)

System.InvalidOperationException: Body was inferred but the method does not allow inferred body parameters.
      Below is the list of parameters that we found: 
      
      Parameter           | Source
      ---------------------------------------------------------------------------------
      y                   | Body (Inferred)
      
      
      Did you mean to register the "Body (Inferred)" parameter(s) as a Service or apply the [FromServices] or [FromBody] attribute?

I want to get this error when starting the application and not when accessing the endpoints for the first time. Does anyone have an idea how I can achieve this behavior?

thanks mMilk

mMilk
  • 245
  • 1
  • 2
  • 10
  • 1
    I don't think is possible because it can't apply a static compilation over a dynamic parameter that will get value at the run time when the endpoint is invoked. – D A Aug 01 '23 at 10:01
  • how do you expect to call your GET /bad and pass a value to as the Value param? You can only use path or/and query params for GET – Roman Marusyk Aug 01 '23 at 10:02
  • @D A thanks for your explanation @Roman Marusyk the example is constructed. It's just that the API is not valid and I want it to be validated at the start of the application and not only at the first access. – mMilk Aug 01 '23 at 10:20

2 Answers2

1

Finally, I found a way to force ASP.NET Core Minimal APIs to throw the exception at startup.

Querying the endpoints causes all endpoints and their RequestDelegates to be created by RequestDelegateFactory which leads to the exception.

A nice solution looks different, but for the beginning it seems to work.

try
{
    ((IEndpointRouteBuilder) app).DataSources.Select(ds => ds.Endpoints).ToList();
}
catch (InvalidOperationException e)
{
   // ... here we go
}

I would welcome a better solution, thanks mMilk

mMilk
  • 245
  • 1
  • 2
  • 10
  • 1
    Personally I would just write an [integration test](https://stackoverflow.com/a/70095604/2501279) for every endpoint, which in general is a more robust approach in general. – Guru Stron Aug 01 '23 at 11:24
0

Your mapGet parameter cannot be set to NotBindable, you should use mapPost

kang
  • 1
  • 1
  • 1
    Thanks @kang, i know what is wrong with the API here and i also know how to fix it :) I would like to know if it is possible and what I would have to do so, that the exception is triggered when the application starts and not when one of the endpoints is accessed for the first time. – mMilk Aug 01 '23 at 09:59
  • You can try to use UnitTest, which will automatically run unit tests after compilation – kang Aug 01 '23 at 14:35
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 07 '23 at 12:48