Kanaka.LifeTimes
1.1.1
dotnet add package Kanaka.LifeTimes --version 1.1.1
NuGet\Install-Package Kanaka.LifeTimes -Version 1.1.1
<PackageReference Include="Kanaka.LifeTimes" Version="1.1.1" />
<PackageVersion Include="Kanaka.LifeTimes" Version="1.1.1" />
<PackageReference Include="Kanaka.LifeTimes" />
paket add Kanaka.LifeTimes --version 1.1.1
#r "nuget: Kanaka.LifeTimes, 1.1.1"
#:package Kanaka.LifeTimes@1.1.1
#addin nuget:?package=Kanaka.LifeTimes&version=1.1.1
#tool nuget:?package=Kanaka.LifeTimes&version=1.1.1
LifeTimes
LifeTimes is a lightweight library that extends the built-in Microsoft.DependencyInjection lifetimes. It provides additional lifetimes for your services, which includes:
Conditional Lifetime – Automatically creates or disposes services based on given condition.
Timed Lifetime – Services that automatically expire after a specified duration or interval.
LifeTimes seamlessly integrates with Microsoft.Extensions.DependencyInjection and follows the same familiar patterns, making it easy to adopt in ASP.NET Core, console apps, or any DI-enabled .NET Core project.
🔨 Installation
LifeTimes is available on NuGet.
dotnet add package Kanaka.LifeTimes
🧩 Usage
The following code demonstrates basic usage of LifeTimes. For a full tutorial see sample Web project in the repository.
ServiceCollection services = new();
services
.AddLifeTime((p, o) =>
{
o.AddTimed<CurrencyService>(p, TimeSpan.FromMinutes(10));
o.AddConditional<TokenService>(p);
}
);
using ServiceProvider provider = services.BuildServiceProvider();
var lifetime = provider.GetService<ILifeTime>();
var currencyService = lifetime.GetService<CurrencyService>();
var rate = currencyService.GetRate("INR");
var tokenService = lifetime.GetService<TokenService>();
var token = tokenService.GetToken();
class CurrencyService
{
private readonly Dictionary<string, decimal> _rates = new();
public CurrencyService()
{
// initialize/update _rates
}
public decimal GetRate(string currency)
{
return _rates.GetValueOrDefault(currency);
}
}
class TokenService : IConditional
{
private readonly string token = string.Empty;
public string GetToken()
{
return token;
}
public bool Condition()
{
var expired = false; // check token expire, for demonstration it's set to false
return expired;
}
}
🏗️ Working Detail
ILifeTime is registered in the the application's DI container. It maintains an internal DI container to manage user-configured service objects. ITypeLifeTime<T>(generic) is a singleton in the internal DI, handling scope creation and disposal of it's service.

💡 Inspiration
The idea for this library came from the podcast Episode of a Lifetime and a blog post by Andrew Lock, which highlighted four additional service lifetimes beyond the standard DI scopes.
🤝 Getting support
If you have a specific question about this project, open a issue with question label. If you encounter a bug or would like to request a feature, submit an issue.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.1 | 176 | 10/30/2025 |