DON'T Use DbContext In Blazor Interactive Server Components! (.NET8)

  Рет қаралды 6,289

Codewrinkles

Codewrinkles

Күн бұрын

I see that there's a lot of confusion on how the new Blazor SSR and the interactive server render mode works under the hood. People are tempted to use the EF Core DbContext in interactive server components, but this will doom you to a future full of exceptions that will be hard to grasp and to fix. In this video I show you exactly how Blazor interactive server components work in .NET 8 and explain how their behavior might affect the DbContext and EF Core. Also I show how we can alternatively use IDbContext factory instead of the regular DbContext and explain why this is definitely a better idea.
#dotnet #blazor #efcore
Join this channel to get source code access and other perks:
/ @codewrinkles
CONTENT
1. Intro: 00:00
2. Understanding the setup: 00:49
3. InteractiveServer Render mode under the hood: 01:51
4. How this affect the DbContext: 06:46
5. IDbContextFactory to the rescue! 11:48

Пікірлер: 56
@naunihalsidhu
@naunihalsidhu 6 ай бұрын
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.
@kell7689
@kell7689 4 ай бұрын
I'd think it's best practice to always use awaits when building user interfaces in general
@ClaudioBernasconi
@ClaudioBernasconi 7 ай бұрын
Interesting video. I haven't used EFCore with Blazor so far, but I will keep it in mind for future projects. It's great to know. Thanks.
@Codewrinkles
@Codewrinkles 7 ай бұрын
Thanks for watching and dropping a comment.
@TosoAlejandro
@TosoAlejandro 6 ай бұрын
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! 👏🏽😄
@Codewrinkles
@Codewrinkles 6 ай бұрын
Glad it helped!
@SayconX2
@SayconX2 Ай бұрын
Nice vídeo!
@gzuares
@gzuares 3 ай бұрын
Excelente gran problema que me has ahorrado. Si ya me estaba dando lata y no sabía cual era el problema.
@sinandoganli
@sinandoganli 6 ай бұрын
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?
@jeandesmarais746
@jeandesmarais746 Ай бұрын
thank you
@saroshwadia
@saroshwadia 7 ай бұрын
Would DbContextFactory work the same in Server/WASM/SSR - so we can use it in all types of Blazor Apps? Thx great video
@Codewrinkles
@Codewrinkles 7 ай бұрын
Well, in wasm you cant't use ef core. You need to make API calls. For the others, yes, you can use the factory in both cases
@saroshwadia
@saroshwadia 7 ай бұрын
@@Codewrinkles yes of course WASM needs to make an API 🙂 - so can the API itself use DbContextFactory so it's always consistent throughout ?
@secury
@secury 4 ай бұрын
Díky!
@Codewrinkles
@Codewrinkles 4 ай бұрын
Thanks for the super!
@ProDevelopmentPK
@ProDevelopmentPK 7 ай бұрын
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?
@ProDevelopmentPK
@ProDevelopmentPK 7 ай бұрын
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.
@Codewrinkles
@Codewrinkles 6 ай бұрын
To me it seems like one of the problems that I described in the video.
@patrickkuhnel9531
@patrickkuhnel9531 7 ай бұрын
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.
@Codewrinkles
@Codewrinkles 7 ай бұрын
That's definitely also a good approach and I like your reasoning.
6 ай бұрын
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.
@kevinlloyd9507
@kevinlloyd9507 6 ай бұрын
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" @
@kevinlloyd9507
@kevinlloyd9507 6 ай бұрын
Actually, I just did some more research and realized what I've implemented already exists: OwningComponentBase. Much better that what I did :)
@marcusmaunula5018
@marcusmaunula5018 6 ай бұрын
What about using Connection Pooling? Have you done a video on that?
@Codewrinkles
@Codewrinkles 6 ай бұрын
I don't recall to have made a video on that topic.
@marcusmaunula5018
@marcusmaunula5018 6 ай бұрын
@@CodewrinklesMight be worth it since the EfCore team seems to favor it over the others.
@marianf25
@marianf25 7 ай бұрын
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?
@Codewrinkles
@Codewrinkles 7 ай бұрын
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.
@marianf25
@marianf25 7 ай бұрын
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 😁
@Codewrinkles
@Codewrinkles 7 ай бұрын
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
@marianf25
@marianf25 7 ай бұрын
@@Codewrinkles Yeah, it seems we'll have to just be careful depending on the situation for now
@user-fqlt
@user-fqlt 5 ай бұрын
What about using dbContext with ServiceLifetime.Transient ?
@RazakRoughneck
@RazakRoughneck 4 ай бұрын
Yeah I run into concurrency issues. I registered the dbcontext as transient and it seems to work.
@MrXLR82
@MrXLR82 6 ай бұрын
Is it efficient to create db context everytime?
@Codewrinkles
@Codewrinkles 6 ай бұрын
Efficiency is the last of your worries if you don't.
@MrXLR82
@MrXLR82 6 ай бұрын
@@Codewrinkles I am thinking to replace everything with factory, is it effective?
@krutelut
@krutelut 6 ай бұрын
amazing video! Is it possible to get the source code for this small project of yours :)
@Codewrinkles
@Codewrinkles 6 ай бұрын
If you get into the Membership program on this channel at ambassador level, you'll always get the source code for each video.
@drumistoyanov
@drumistoyanov 6 ай бұрын
I was having this trouble a few days ago. Wasted 5 hours to find out what was the problem...
@Codewrinkles
@Codewrinkles 6 ай бұрын
I also learned this the hard way!
Escaping The .NET 8 Blazor Redirect To Login Hell
4:29
Codewrinkles
Рет қаралды 5 М.
Blazor Server Full CRUD Operations From Scratch Incl. EF Core
30:39
Codewrinkles
Рет қаралды 4,8 М.
Каха инструкция по шашлыку
01:00
К-Media
Рет қаралды 8 МЛН
Increíble final 😱
00:37
Juan De Dios Pantoja 2
Рет қаралды 65 МЛН
Omega Boy Past 3 #funny #viral #comedy
00:22
CRAZY GREAPA
Рет қаралды 37 МЛН
Everything You Need To Know About Blazor in .NET 8
21:18
Codewrinkles
Рет қаралды 11 М.
Await Async Tasks Are Getting Awesome in .NET 9!
9:24
Nick Chapsas
Рет қаралды 87 М.
Ten Amazing Blazor Features You Must Know
25:34
Gavin Lon
Рет қаралды 5 М.
Microfrontends with Blazor
13:16
Codewrinkles
Рет қаралды 6 М.
Why Do C# Developers Hate The var Keyword?
11:15
Nick Chapsas
Рет қаралды 60 М.
Background Jobs in ASP.NET Core
18:35
IAmTimCorey
Рет қаралды 45 М.
5 НЕЛЕГАЛЬНЫХ гаджетов, за которые вас посадят
0:59
Кибер Андерсон
Рет қаралды 1,6 МЛН
DC Fast 🏃‍♂️ Mobile 📱 Charger
0:42
Tech Official
Рет қаралды 481 М.
Мечта Каждого Геймера
0:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 1,3 МЛН
📦Он вам не медведь! Обзор FlyingBear S1
18:26
Урна с айфонами!
0:30
По ту сторону Гугла
Рет қаралды 6 МЛН
Bardak ile Projektör Nasıl Yapılır?
0:19
Safak Novruz
Рет қаралды 6 МЛН