Skip to content

Use InferFromServicesAttribute to infer non-interface parameters in minimal actions come from services #32563

Closed
@halter73

Description

@halter73
builder.Services.AddDbContext<ApplicationDbContext>(
// ...

app.MapPost("/todos", (ApplicationDbContext context) => { 
// ...

Today, the above call to MapPost would attempt to deserialize the ApplicationDbContext from a JSON body because there's no attribute and ApplicationDbContext is a non-interface, non-TryParsable type. Adding [FromServices] to the parameter fixes the issue, but it would be nice if for certain non-interface, non-TryParsable types like types derived from DbContext were inferred to come from services by default.

One way we could do this is by looking at each parameter type and checking if that type or any of its parents have a certain attribute. Maybe we could call it InferFromServicesAttribute. Because we want libraries like EF to use this without needing to take a dependency on ASP.NET Core packages, we probably want to look for this attribute by name.

@ajcvickers

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-minimal-actionsController-like actions for endpoint routing

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions