Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
In a Minimal API app using EF Core, enabling validation with builder.Services.AddValidation()
causes endpoint methods to fail with a 500 Internal Sever Error
because of an exception thrown in the validation logic.
Related to #61770
Expected Behavior
No exception occurs.
Steps To Reproduce
Create a minimal API, such as a Todo app, where the data is stored in an EFCore DB -- I used sqlite.
Pass the DB context into the methods using an AsParameters
class like this:
group.MapGet("/", async ([AsParameters] TodoServices services) =>
await services.Db.Todos.ToListAsync());
where TodoServices
is defined as
public class TodoServices(TodoContext db)
{
[FromServices]
public TodoContext Db => db;
}
Of course, you have to inject the DB Context into the DI container in the Program.cs
, like this:
builder.Services.AddDbContext<TodoContext>(options =>
options.UseSqlite("Data Source=todos.db"));
For testing purposes, it helps to also seed the database with a few Todo entries.
This app should run fine when validation is not enabled. For example, a GET to the endpoint shown above should return a list of Todos.
Then enable validation by adding the validation services to the DI container in Program.cs
like this:
// Add validation services to the DI container.
builder.Services.AddValidation();
With this change, the GET endpoint shown above will fail with a 500 Internal Server Error
because the validation logic threw an exception.
A complete repro of the issue is available in the validation-and-efcore directory in the preview5 branch of this repo:
https://github.com/mikekistler/dotnet10-issue-repros
Exceptions (if any)
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Property 'Microsoft.EntityFrameworkCore.Storage.IDatabaseFacadeDependenciesAccessor.Dependencies' not found on type 'DatabaseFacade'.
at Microsoft.AspNetCore.Http.Validation.ValidatablePropertyInfo.ValidateAsync(Object value, ValidateContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Validation.ValidatableTypeInfo.ValidateAsync(Object value, ValidateContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Validation.ValidatablePropertyInfo.ValidateAsync(Object value, ValidateContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Validation.ValidatableTypeInfo.ValidateAsync(Object value, ValidateContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Validation.ValidatableTypeInfo.ValidateAsync(Object value, ValidateContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Validation.ValidatablePropertyInfo.ValidateAsync(Object value, ValidateContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Validation.ValidatableTypeInfo.ValidateAsync(Object value, ValidateContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Validation.ValidatableParameterInfo.ValidateAsync(Object value, ValidateContext context, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Validation.ValidationEndpointFilterFactory.<>c__DisplayClass0_0.<<Create>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Http.RequestDelegateFactory.<ExecuteValueTaskOfObject>g__ExecuteAwaited|129_0(ValueTask`1 valueTask, HttpContext httpContext, JsonTypeInfo`1 jsonTypeInfo)
at Microsoft.AspNetCore.Diagnost
.NET Version
10.0.100-preview.5.25269.109
Anything else?
No response