Kanaka.LifeTimes 1.1.1

dotnet add package Kanaka.LifeTimes --version 1.1.1
                    
NuGet\Install-Package Kanaka.LifeTimes -Version 1.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Kanaka.LifeTimes" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Kanaka.LifeTimes" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Kanaka.LifeTimes" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Kanaka.LifeTimes --version 1.1.1
                    
#r "nuget: Kanaka.LifeTimes, 1.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Kanaka.LifeTimes@1.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Kanaka.LifeTimes&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Kanaka.LifeTimes&version=1.1.1
                    
Install as a Cake Tool

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.

Working Detail

💡 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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