In .NET Core, we can control the lifetime of our services, including
DbContext, by specifying the appropriate
ServiceLifetime. The choice of service lifetime depends on application’s requirements.
Here are some common service lifetimes and when to use them:
Transient: A new instance of the service is created every time it’s requested. This is suitable for stateless services or services that have a very short lifespan.
Scoped: A single instance of the service is created for each HTTP request. Scoped services are suitable for scenarios where you want to share the same instance of a service within the scope of a single request. In ASP.NET Core, this is often used for
DbContextinstances in a web application. Each HTTP request gets its own
DbContext, and the
DbContextis disposed at the end of the request.
Singleton: A single instance of the service is created for the lifetime of the application. This is suitable for stateless services that can be reused across all requests.
I only have experience with C# Worker service to consume messsage from a queue. For
DbContext instances, I usually choose
Transient because my C# worker working independently with each message.
When using Transient lifetime, a new instance of the DbContext will be created for each operation, ensuring that each message is handled in isolation without sharing the same DbContext instance across messages.
Here’s an example of registering a scoped
DbContext in .NET Core
- #c sharp
my note about C Sharp ServiceLifeTime
Having too many unused or underused indexes on a table can slow down write and update operations in your PostgreSQL database, making it crucial to regularly identify and manage them for optimal performance.
Newsletter #6 - Pegasus, Ruby, PostgreSQL and networkQuality tool
According to Google's Material Design, keep paragraph spacing in the range between .75x and 1.25x of the type size.
Comparison between `TransferUtility.DownloadAsync`, `DownloadSingleFileAsync`, and `GetObjectAsync`.