There is a performance issue with WeatherForecastController.cs
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
In the templates of ASP.NET Core Web API, the Get() method of 'aspnetcore/src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Controllers/WeatherForecastController.cs ’ is as follows:
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
The return value of Get() method is of IEnumerable<WeatherForecast>, and the Select() method returns the same type, so there was no need to convert it to an array using ToArray(). Furthermore, it can cause huge memory consumption problem, and I have stated it in a post: https://www.reddit.com/r/aspnetcore/comments/z7jk8r/who_says_net_doesnt_have_gc_tuning_changing_one/
Describe the solution you’d like
I suggest that remove the invocation of ‘ToArray()’, which could set up a better model for .NET developers.
Additional context
No response
Issue Analytics
- State:
- Created 10 months ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Troubleshoot Performance Bottlenecks in .NET 6 ...
In this take, I'll explore performance bottlenecks in a .NET 6 application. The focus will be on a performance issue I've personally seen...
Read more >ASP.NET Web API performance issue
As I've used Stopwatch to test data-retrieval performance, it's apparent that it takes less than a second. However, when I issue a request ......
Read more >Rate limiting in .NET Core APIs - Onur KARAKUŞ - Medium
This will result in excessive resource consumption and performance issues. The applications we create can process a certain number of requests based on...
Read more >Exploring the dotnet new Web API Template
In this lesson, we'll start building a Cretaceous Park API. This API will share data about a wildlife park consisting of creatures from...
Read more >Build and run an ASP.NET Core app in a container
Develop, build, and debug an ASP.NET Core app in a Docker container, using Visual Studio Code.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks for your reply. The official templates should set up a good example for developers, and the redundant ‘ToArray()’ should be removed. Redundant is redundant. Any misleading usage from officials is intolerable.
Removing ‘ToArray()’ is no harm but good. You didn’t convince me, but I don’t want to insist. Good day!