FachIT360.Lifetime 0.1.9

dotnet add package FachIT360.Lifetime --version 0.1.9
                    
NuGet\Install-Package FachIT360.Lifetime -Version 0.1.9
                    
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="FachIT360.Lifetime" Version="0.1.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FachIT360.Lifetime" Version="0.1.9" />
                    
Directory.Packages.props
<PackageReference Include="FachIT360.Lifetime" />
                    
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 FachIT360.Lifetime --version 0.1.9
                    
#r "nuget: FachIT360.Lifetime, 0.1.9"
                    
#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 FachIT360.Lifetime@0.1.9
                    
#: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=FachIT360.Lifetime&version=0.1.9
                    
Install as a Cake Addin
#tool nuget:?package=FachIT360.Lifetime&version=0.1.9
                    
Install as a Cake Tool

FachIT360.Lifetime

A small .NET utility for binding cleanup actions, disposable resources, event subscriptions, and asynchronous operations to the lifetime of an owning object.

FachIT360.Lifetime provides a simple LifetimeScope that helps keep cleanup logic close to the code that creates or registers resources.

Features

  • Register cleanup actions
  • Register IDisposable resources
  • Subscribe and automatically unsubscribe from events
  • Use a lifetime-bound CancellationToken
  • Dispose all registered cleanup actions in reverse registration order
  • No dependency on Blazor, ASP.NET Core, dependency injection, or logging

Installation

dotnet add package FachIT360.Lifetime

Basic usage

using FachIT360.Lifetime;

public sealed class MyService : IDisposable
{
    private readonly LifetimeScope _lifetime = new();

    public MyService(SomeEventSource source)
    {
        // Subscribe register and unregister actions                                                                                                                                   1
        _lifetime.Subscribe(
        () => source.Changed += OnChanged,
        () => source.Changed -= OnChanged);
    }

    private void OnChanged()
    {
        // React to event
    }

    public void Dispose()
    {
        _lifetime.Dispose();
    }
}

Add Custom Cleanup actions

Use AddCleanup for custom cleanup logic that should run when the scope is disposed.

_lifetime.AddCleanup(() => Console.WriteLine("Cleaning up!"));

Add Disposable resources

Use AddDisposable for resources that implement IDisposable.

var timer = new Timer(_ => DoWork(), null, TimeSpan.Zero, TimeSpan.FromSeconds(10));
_lifetime.AddDisposable(timer);

Add Event subscriptions

Use Subscribe to register an action immediately and provide the matching cleanup action.

_lifetime.Subscribe(
() => service.Changed += OnChanged,
() => service.Changed -= OnChanged);

The first action is executed immediately. The second action is executed when the lifetime scope is disposed.

This pattern is useful for event subscriptions because it keeps subscription and unsubscription next to each other.

Cancellation token

LifetimeScope.Token is canceled when the scope is disposed.

await dbRepository.LoadAsync(_lifetime.Token);

Use this token for asynchronous operations that should stop when the owning object is disposed.

The token represents the lifetime of the owning object. It is not intended to control individual operations such as canceling a single search or uplo ad. For operation-specific cancellation, use a separate CancellationTokenSource.

Disposal behavior

When LifetimeScope.Dispose() is called:

  1. The lifetime token is canceled.
  2. Registered cleanup actions are executed in reverse registration order.
  3. Cleanup exceptions are ignored.
  4. The internal CancellationTokenSource is disposed.

Intended use

  • Services
  • Workers
  • View models, provided no dependencies arise (e.g., WPF, WinForms, MAUI, etc.)
  • Background tasks
  • Coordinator objects
  • Domain objects
  • Any object with a deterministic lifetime

The owning object should dispose the scope when its own lifetime ends.

FachIT360.Lifetime.Blazor

FachIT360.Lifetime.Blazor integrates the LifetimeScope utility into the lifecycle of Blazor components and layouts. It provides deterministic cleanup, cancellation mechanisms tied to the lifetime, and optional support for typed logging for Blazor Server and WebAssembly.

This package extends the core library FachIT360.Lifetime, adding abstractions specifically tailored to Blazor applications.

Installation

dotnet add package FachIT360.Lifetime.Blazor

The FachIT360.Lifetime.Blazor package already references FachIT360.Lifetime internally and does not need to be specified explicitly.

FachIT360.Lifetime.Blazor — API Overview

Component Inheritance Hierarchy

ComponentBaae
    ↓
LifetimeComponentBase
    ↓
LifetimeComponentBase<TComponent>

Layout Inheritance Hierarchy

LayoutComponentBase
    ↓
LifetimeLayoutBase
    ↓
LifetimeLayoutBase<TComponent>

Members

protected LifetimeScope Lifetime { get; }
protected CancellationToken CancellationToken { get; } 
protected virtual void OnDisposeManaged();

Usage Examples

public sealed partial class Dashboard : LifetimeComponentBase<Dashboard>
{
    protected override async Task OnInitializedAsync()
    {
        Lifetime.Subscribe(
            () => UiState.Changed += OnUiStateChanged,
            () => UiState.Changed -= OnUiStateChanged);

        var data = await DataService.LoadAsync(CancellationToken);

        Logger.LogInformation("Dashboard initialized.");
    }
}

Intended Use

FachIT360.Lifetime.Blazor is intended

  • Blazor components with event subscriptions
  • Components using JS interop
  • Layouts with shared UI state
  • Components with long-running asynchronous operations
  • Components requiring deterministic cleanup logic

Usage in a layout

@inherits LifetimeLayoutBase<MainLayout>

<div class="page">
    <main>
        @Body
    </main>
</div>

@code {
    protected override void OnInitialized()
    {
        Logger.LogInformation("MainLayout initialized.");

        _ = SomeService.LoadAsync(CancellationToken);
    }
}

License

MIT

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.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on FachIT360.Lifetime:

Package Downloads
FachIT360.Lifetime.Blazor

A small .NET lifetime scope utility for cleanup actions, disposable resources, subscriptions, and lifetime-bound cancellation for Blazor applications.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.9 110 8/16/2026
0.1.8 134 7/11/2026
0.1.7 141 7/7/2026
0.1.6 122 7/7/2026
0.1.5 145 7/5/2026
0.1.4 123 7/5/2026