dotnet / dotnet/aspnetcore

[Analyzer] Warn about returning `IEnumerable<T>` from action or minimal API

Open
#44,391 5 comments 2 reactions 0 assignees View on GitHub
analyzer area-minimal investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

> Returning `IEnumerable` from an action results in synchronous collection iteration by the serializer. The result is the blocking of calls and a potential for thread pool starvation. To avoid synchronous enumeration, use ToListAsync before returning the enumerable.

See https://learn.microsoft.com/en-us/aspnet/core/performance/performance-best-practices?view=aspnetcore-6.0#return-ienumerablet-or-iasyncenumerablet

### Describe the solution you'd like

An analyzer that warns when `IEnumerable` is returned from a controller or minimal API.

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.