The Right Way To Use Scoped Services From Singletons in ASP.NET Core

  Рет қаралды 9,150

Milan Jovanović

Milan Jovanović

Күн бұрын

Get the source code for this video for FREE → the-dotnet-weekly.ck.page/sco...
☄️ Master the Modular Monolith Architecture: bit.ly/3SXlzSt
📌 Accelerate your Clean Architecture skills: bit.ly/3PupkOJ
🚀 Support me on Patreon to access the source code: / milanjovanovic
Did you ever need to inject a scoped service into a singleton service? I often need to resolve a scoped service, like the EF Core DbContext, in a background service. Another example is when you need to resolve a scoped service in ASP.NET Core middleware. If you ever tried this, you were probably greeted with an exception. In this video, I'll explain how you can solve this problem and safely use scoped services from within singletons in ASP.NET Core.
Check out my courses: bit.ly/3PupkOJ
Using Scoped Services From Singletons in ASP.NET Core
www.milanjovanovic.tech/blog/...
Join my weekly .NET newsletter:
www.milanjovanovic.tech
Read my Blog here:
www.milanjovanovic.tech/blog
Chapters
0:00 Injecting Scoped services in Background jobs
3:07 Resolving Scoped services with IServiceScopeFactory
4:55 IServiceProvider vs IServiceScopeFactory
6:08 Injecting Scoped services in Middleware

Пікірлер: 28
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Get the source code for this video for FREE → the-dotnet-weekly.ck.page/scoped-singleton
@slvberg
@slvberg 3 ай бұрын
I just faced this challenge two days ago when implementing hosted services, and was temporarily registering the scoped services as singletons to make it compile. Planned to head back later to deal with it properly. I was immediately interested, and very surprised when I saw your newsletter dropping in my email. Could believe you were watching me. Thank you so much for sharing this!
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Just happy to help. These are common problems most of us run into (sooner or later)
@windssoul
@windssoul 3 ай бұрын
It’s very strange because a few months ago I experienced the same issue when creating an API gateway for our application and had to restructure the dependency injection to handle it with the scoped service factory after trial and error. Then a few weeks later this video pops up lol. So at least I confirm you butt up against this issue almost inevitably.
@acartunahan
@acartunahan Ай бұрын
Thank you Milan. As a junior developer, this video helped me a lot!
@MilanJovanovicTech
@MilanJovanovicTech Ай бұрын
Glad it helped!
@Robert-yw5ms
@Robert-yw5ms 3 ай бұрын
This also solves a similar issue in blazor server when you want to inject a service that ultimately relies upon dbcontext into a blazor component. The issue being that every socket connection has its own scope that persists as long as the user is connected.
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Haven't done a lot of Blazor, but I can imagine it's quite similar to how other SPA frameworks treat a "scope"
@mylesdavies9476
@mylesdavies9476 3 ай бұрын
Useful recap, thanks 🙂
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Glad it was helpful!
@10Totti
@10Totti 3 ай бұрын
Nice tutorial!
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Thanks!
@Cornet435
@Cornet435 3 ай бұрын
But what about making middleware with IMiddleware interface nad then register it as a scoped? Then u can inject dbcontext
@walidchouaib8526
@walidchouaib8526 3 ай бұрын
Thats what i am trying to do to rollback transaction whenever an exception occurred
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
That probably works also (haven't checked, though)
@primelimitt
@primelimitt 3 ай бұрын
hello great video! I have one question, why does it matter for the db if the scoped instance is different ? I mean we only have 1 db, so it shouldnt matter what is the scope for us. does this work the same with a db that is not in memory? thanks
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
The DbContext will eat up a lot of memory if it's not scoped. Plus there's also the problem of change tracking.
@usmanali-xz5ub
@usmanali-xz5ub 3 ай бұрын
How do you learn so many things ? Is there any book ?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
Solve many problems
@fataleroro2011
@fataleroro2011 3 ай бұрын
🎉
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
💪
@sergeu90
@sergeu90 2 ай бұрын
I don't undertand why we don't see changes in database after SaveChangesAsync when we create scope inside InvokeAsync. Coud you please explain this part on the kzbin.info/www/bejne/fITNdHqaoMeceaMsi=wHhjpr0UgAzO0hFI&t=613 this time. Thanks for video. It is very interesting.
@MilanJovanovicTech
@MilanJovanovicTech 2 ай бұрын
It's two different DbContext instances
@devliren
@devliren 3 ай бұрын
:)
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
😁😁
Getting Started with Modular Monoliths in .NET
12:37
Milan Jovanović
Рет қаралды 20 М.
How to Implement API Versioning for Minimal APIs | ASP.NET Core 8
18:11
Milan Jovanović
Рет қаралды 12 М.
OMG😳 #tiktok #shorts #potapova_blog
00:58
Potapova_blog
Рет қаралды 3,9 МЛН
"Stop Using Async Await in .NET to Save Threads" | Code Cop #018
14:05
Domain Modeling with Domain-Driven Design (From Scratch)
18:05
Milan Jovanović
Рет қаралды 23 М.
Why I Quit the Scrum Alliance
7:58
The Passionate Programmer
Рет қаралды 9 М.
ASP.NET Core - Service Scope - Singleton vs Scoped vs Transient
10:11
EF Core Migrations Deep Dive, Applying Migration, SQL Scripts
16:41
Milan Jovanović
Рет қаралды 10 М.
"Stop Using Automapper in .NET!" - Code Cop #001
9:57
Nick Chapsas
Рет қаралды 108 М.
Background Jobs in ASP.NET Core
18:35
IAmTimCorey
Рет қаралды 46 М.
Forget Controllers and Minimal APIs in .NET!
14:07
Nick Chapsas
Рет қаралды 53 М.
Scheduling repeating tasks with .NET 6’s NEW Timer
12:47
Nick Chapsas
Рет қаралды 104 М.
iPhone 12 socket cleaning #fixit
0:30
Tamar DB (mt)
Рет қаралды 54 МЛН
Хотела заскамить на Айфон!😱📱(@gertieinar)
0:21
Взрывная История
Рет қаралды 4,4 МЛН
💅🏻Айфон vs Андроид🤮
0:20
Бутылочка
Рет қаралды 740 М.