First of all great video highlighting the DbContext issue in Blazor Server(.NET 6/7) and now in .NET for Interactive Server Side Rendering. I encountered it back in .NET 6 and was able to resolve it back than using the technique you mentioned here. I know it takes lot of time to create video and sharing it for the community, great work and great topic !. I also noticed that every time we run EF query or any operation on EF we should always use await ... EF Operation, otherwise it can run into threading issue.
Thank you for your video! I found the solution to my issue with DbContext in Blazor Interactive Server. It worked perfectly with the solution from your video. I had no idea what could be causing the error in my application; it was functioning correctly until it suddenly stopped, throwing some very strange errors. I had no clue until I started searching online and happened to come across your video. Great help! 👏🏽😄
I made an app in blazor server. If i display an invoice # 1 with detail rows and switch to invoice #2. then i if i go to DB back end and change Qty from 1 to 2 of Invoice #1 using update query. Now if i retrieve invoice #1 again in UI it still shows me old Qty and not Qty=2. Ever since I am using DBContextFactory this problem is gone.
If we do not use Ef and DBcontext, but perform our operations using system.data.sqlclient with a helper we wrote and dispose at the end of the transaction, will we encounter the same problem?
If I would have seen this video 3 years ago I would now have in total at least 1 week of my life back 🤣 btw I'm curious how do you handle repositories/services with this approach?
You inject the IDbContextFactory instead of the DbContext and than you do exactly what I didin the video. However, there is a certain level of awkwardness here (that I mentioned in the previous video) because your app will now have a lot of places where you can use the DbContext (regular SSR components) and a lof of places where you would need to use IDbContextFactory. That's in fact one of the major downsides of this new Blazor model.
That’s how I ended up doing it. I also find it a bit awkward, I was hoping someone has a more elegant solution. Maybe Microsoft will handle it in .Net 9 😁
Doesn't seem like that.Take a look at this Twitter thread. David Fowler doesn't seem to agree that there is a problem (maybe not problem, but very bad dev experience): twitter.com/danpdc/status/1724596015087357955
I inject the service provider to my blazor components and manually create scopes in my components and get the services from these scopes. Are there any drawbacks with this? I don't like the idea of changing Backend code because some frontends behave different than others. The issue is not the Backend it's the frontend, so that's where I fix it.
That's definitely also a good approach and I like your reasoning.
9 месяцев назад
Also just create a base component for your components and you have auto-scoping. That is what Blazor should have provide, auto scoping so that all the three render modes work the same.
I did the same thing last week on a project. Used a base component that would handle it's own scopes and resolve it's own services and just dispose them in the IDispose() of the component. I have a lot of existing backend services that rely on the DbContext being a Unit Of Work shared among services. And DbContextFactory wasn't going to provide the fix I needed. I'm curious how you implemented your "auto scoping" @
Nice, I've a Global InteractiveServer App which uses Repository Pattern and Services. I Inject IUserService in my Component and that Service use UnitOfWork that handles DbContext. I've faced exceptions while navigating to multiple components that uses DbContext. Do you think If I just replace builder.Services.AddDbContext to AddDbContextFactory then it will resolve my issue?
When I switch b/w multiple components that use Services>DbContext I get this error: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext.