question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

There is a performance issue with WeatherForecastController.cs

See original GitHub issue

Is 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:closed
  • Created 10 months ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
yangzhongkecommented, Nov 29, 2022

This isn’t even using a database. It’s the least real example you could imagine. This is just a sample to get people started. Hopefully, they run the app and delete the endpoint and don’t try to return 150,000 random weather forecasts unpaged from their endpoints 😁

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.

0reactions
yangzhongkecommented, Nov 30, 2022

Removing ‘ToArray()’ is no harm but good. You didn’t convince me, but I don’t want to insist. Good day!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found