Skip to content
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

Blazor Server framework results in multiple threads hitting Entity Framework Context #17004

Closed
vsfeedback opened this issue Nov 12, 2019 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components question

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


I am familiar with writing highly responsive ASP.NET MVC applications where we load the initial page data, and then fire off multiple AJAX calls to load "secondary" data for the page. So behinds the scenes, these AJAX requests all hit the web server(s) about the same time and pull data from our SQL DB.

I envisage doing something very similar with a Blazor application. So, we'd load the page with some data and then have multiple components then display on the page with additional data; the loading of these components could be initialized by a user action, or by some other event(s) on the page.

And that's where I see a problem: the event handlers all react very nicely to events fired by the various components on the razor page, and they all then hit our DataService (injected into the page) which then results in lots of threads hitting our Entity Framework's Context.

And that's not allowed - only one thread may call the context at any one time.

To get around this, I can lock the Context using a SemaphoreSlip(1,1) to ensure that only one thread gets access at any one time, but that seems a bit of a bottle neck to me. Surely locking the Context is not something that Microsoft would be expecting us to do....??

So what am I missing here?

My setup
I'm using Blazor-Server, .NET Core 3.1/EF Core 3.1. In my App's startup, I'm setting up the EF Context and the Service for DI:

services. AddDbContext<MyContext>();
services. AddScoped<MyService>();

The Context is injected into the constructor of the Service:

public MyService(MyContext context)
{
    this. Context = context;
}
private MyContext Context { get; }

MyService has a bunch of async/awaited calls, each using the same context. Although MyContext is deliberately small, I can't see us wanting to instantiate it for every call, hence why it's using the constructor-injected instance.

Oh, and of course, the Service is injected into the *.razor page.


Original Comments

Visual Studio Feedback System on 11/4/2019, 10:38 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Nov 12, 2019
@javiercn
Copy link
Member

Hi,

This is a duplicate of #16745

See #16745 (comment) for details on how to fixed and the linked sample here for the right pattern to follow when using Entity Framework with Blazor. https://github.com/aspnet/AspNetCore/files/3805645/EFReproCorrectPattern.zip

@DrGriff
Copy link

DrGriff commented Nov 12, 2019

The sample provided above is for a Client architecture. What about Server architecture?

I subsequently found another relevant issue: Using Entity Framework Core with Blazor that contains suggestions which seem to work for me.

@javiercn
Copy link
Member

@DrGriff It works the same way. The issue was happening in a prerendering context in which the code runs on the server. Using OwningComponentBase<T> is still the answer.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 14, 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

4 participants