Skip to content

Using DI with Razor Components lead to holding on to resources for long times #7396

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

Closed
matthewDDennis opened this issue Feb 8, 2019 · 5 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components question

Comments

@matthewDDennis
Copy link

If I understand the way DI is used in Razor Components, Scoped services have a lifetime of the connection.
Wouldn't this, especially in the case of DbContexts, leave a lot of resources alive for long times and result in resource, such as SQL connections, or memory starvation if there are MANY concurrent requests?

@muratg muratg added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Feb 8, 2019
@mkArtakMSFT mkArtakMSFT added area-blazor Includes: Blazor, Razor Components question labels Feb 9, 2019
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @matthewDDennis.
@rynowak, can you please answer this question? Thanks!

@mkArtakMSFT
Copy link
Member

Seems related: #5496

@mkArtakMSFT
Copy link
Member

@matthewDDennis, you're right about scoped services to use resources throughout the lifetime of the connection, so it's important to be mindful about the usage of those resources.
Also, we plan to implement a mechanism to evict idle connections so resources aren't wasted. We track that work as part of #6358.

@vertonghenb
Copy link
Contributor

Related: aspnet/Blazor.Docs#400
In some cases Scoped is really wanted, for example to keep AppState objects to hold the state of the UI for the entire scope of the connection. However for EF etc, I wouldn't advise to use a Scoped DbContext but rather use a transient one.

@Andrzej-W
Copy link

@matthewDDennis as @vertonghenb has already written you should use transient scope. As long as your business logic is within single function it is probably not a problem. Otherwise you have to get your context in one place and then pass it as a parameter to other functions or constructors.

Warning: you should get your context in a function and assign it to local variable in that function. Don't use automatic constructor injection and don't assign transient context to the field in a class. In Razor Components your class will be life at least as long as user interacts with current page. I don't use Razor Components, but I'm almost sure that I'm right here.

@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components question
Projects
None yet
Development

No branches or pull requests

6 participants