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:

  1. 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.

  2. 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 DbContext instances in a web application. Each HTTP request gets its own DbContext, and the DbContext is disposed at the end of the request.

  3. 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 Program.cs:

// Assuming we have a ExampleDbContext class
services.AddDbContext<ExampleDbContext>(options =>
    options.UseNpgsql(connectionString, db => db.EnableRetryOnFailure(5));
}, ServiceLifetime.Transient);