ServiceLifetime

Describes the lifetime of a service.

Values

ValueMeaning
Transient

The service is constructed every single time it is requested.

These services are the most expensive to use, as they need to be constructed *every single time* they're requested, which also puts strain on the GC.

Singleton

The service is only constructed a single time for every ServiceProvider, regardless of which scope is used to access it.

These services are the least expensive to use, as they are only constructed a single time per ServiceProvider.

Scoped

The service is constructed once per scope.

These services are between Transient and Singleton in terms of performance. It mostly depends on how often scopes are created/destroyed in your program.

See_Also: ServiceProvider.createScope

Meta