filmov
tv
Why singleton hates scoped injection in .NET dependency injection
Показать описание
Singleton, scoped and transient are the service lifetimes available when using dependency injection in .NET.
Most services can inject other services regardless of their lifetime. However, if you attempt to inject a scoped service into a singleton, you get a "Cannot consumed scoped service" error.
This can cause a problem if you're using Entity Framework for a database query in a service with a singleton lifetime, like a background service or SignalR.
We'll have a look at why this is and how we can use the IServiceProvider instance to create a custom scope so that we are able to use services that have a scoped lifetime.
📖 Learn .NET and C# with our online courses 📖
► More information:
► Chapters
0:00 Overview of the problem
0:24 Learn dependency injection with our online course
0:34 Service injection
1:44 Why can't a singleton inject a scoped?
2:14 When is this a problem?
2:50 Demo on the problem and how to resolve
5:18 Learn dependency injection with our online course
Most services can inject other services regardless of their lifetime. However, if you attempt to inject a scoped service into a singleton, you get a "Cannot consumed scoped service" error.
This can cause a problem if you're using Entity Framework for a database query in a service with a singleton lifetime, like a background service or SignalR.
We'll have a look at why this is and how we can use the IServiceProvider instance to create a custom scope so that we are able to use services that have a scoped lifetime.
📖 Learn .NET and C# with our online courses 📖
► More information:
► Chapters
0:00 Overview of the problem
0:24 Learn dependency injection with our online course
0:34 Service injection
1:44 Why can't a singleton inject a scoped?
2:14 When is this a problem?
2:50 Demo on the problem and how to resolve
5:18 Learn dependency injection with our online course
Why singleton hates scoped injection in .NET dependency injection
Understanding Dependency Injection in .NET: Transient vs Scoped vs Singleton Explained
ASP.Net Core: How to inject scoped services into singleton services (or background services)
❌️ ASP.NET Dependency Injection: You may be wrong about Scoped services!
C# : Cannot consume scoped service IMongoDbContext from singleton IActiveUsersService after upgrade
C# : Cannot consume scoped service 'MyDbContext' from singleton 'Microsoft.AspNetCore...
Mastering Dependency Injection Scopes in .NET Core : Scoped, Transient and Singleton Explained
AddTransient, AddScoped and AddSingleton Services Differences
Dependency Injection Is Now Complete In .NET 8!
.NET Dependency Injection Tips and Tricks
exception error Singleton code C # MVC Asp Net 6 Startup
Retiring the Singleton Pattern, Concrete Suggestions for What to Use Instead - Pete Muldoon ACCU 21
.NET 5 Dependency Injection
What Are Singletons and How Do You Use Them?
Utilities, Singletons and Dependency Injection - Effective Java, Items 3-5
Avoid This Exception!
Are You Using the DbContext in Background Services PROPERLY?
05 Singleton Design Pattern In C#
The Ugly side of Dependency Injection
C# : Singleton httpclient vs creating new httpclient request
RubyConf 2019 - The Singleton Module and Its Pattern In Ruby by Mike Calhoun
Singleton Pattern in C#
The Common Entity Framework Mistake You MUST Fix
Dot NET Notts - April 2020 - Matt Weston - Power Automate + Steve Collins - Dependency Injection
Комментарии