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
Fixer/Analyzer: Hoist explicit calls to HttpContext.RequestServices.GetService to route handler parameters #35812
Comments
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Thanks for contacting us. We're moving this issue to the |
Background and MotivationMany developers are unaware that request services can be resolved as parameters from minimal route handlers. This includes route handlers that were previously just Proposed AnalyzerWe should have a fixer that rewrites calls to GetRequiredService to injecting them directly into handler methods. Analyzer Behavior and MessageCode should go from this: app.MapGet("/", (HttpContext context) =>
{
var service = context.RequestServices.GetRequiredService<ITodoService>();
return service.GetTodos();
}); To this: app.MapGet("/", (ITodoService service) =>
{
return service.GetTodos();
}); Or to reduce the likelihood of changing behavior, we could use the app.MapGet("/", ([FromServices] ITodoService service) =>
{
return service.GetTodos();
}); The message could be.
Category
Severity Level
Usage Scenarios// RequestDelegate?
app.MapGet("/refresh", async (context) =>
{
var service = context.RequestServices.GetRequiredService<ITodoService>();
await service.RefreshAsync();
});
// Optional services?
app.MapGet("/", (HttpContext context) =>
{
var service = context.RequestServices.GetService<ITodoService>();
return service?.GetTodos() ?? Array.Empty<Todo>();
});
// typeof(T)
app.MapGet("/", (HttpContext context) =>
{
var service = context.RequestServices.GetRequiredService(typeof(ITodoService));
return service.GetTodos();
});
// Other MapXXX methods
app.MapPost("/", (HttpContext context, TodoDto todo) =>
{
var service = context.RequestServices.GetRequiredService(typeof(ITodoService));
return service.GetTodos();
});
app.MapDelete("/{id}", (HttpContext context, int id) =>
// ... RisksSome people might prefer plain-old When the result of If nullability annotations are not enabled for the project, it might not be possible to correctly annotate the |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
We should have a fixer that rewrites calls to GetRequiredService to injecting them directly into handler methods. Code should go from this:
To this:
The text was updated successfully, but these errors were encountered: