Extensions.DependencyInjection.SessionScoped 9.0.2

dotnet add package Extensions.DependencyInjection.SessionScoped --version 9.0.2                
NuGet\Install-Package Extensions.DependencyInjection.SessionScoped -Version 9.0.2                
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="Extensions.DependencyInjection.SessionScoped" Version="9.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Extensions.DependencyInjection.SessionScoped --version 9.0.2                
#r "nuget: Extensions.DependencyInjection.SessionScoped, 9.0.2"                
#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.
// Install Extensions.DependencyInjection.SessionScoped as a Cake Addin
#addin nuget:?package=Extensions.DependencyInjection.SessionScoped&version=9.0.2

// Install Extensions.DependencyInjection.SessionScoped as a Cake Tool
#tool nuget:?package=Extensions.DependencyInjection.SessionScoped&version=9.0.2                

builder.Services.AddSessionScoped<T>();

A lightweight extension for managing session-scoped data in ASP.NET Core applications.

Overview

SessionScoped simplifies session management by providing scoped data storage tied to the user's session. It integrates seamlessly with ASP.NET Core's dependency injection, making session-based state management easy and robust.

Features

  • Scoped data tied to the user's session.
  • Fully compatible with ASP.NET Core dependency injection.
  • Minimal setup required.

Getting Started

  1. Create a Service implementing IHostedService interface
public class ExampleService(ILoggerFactory loggerFactory) : IHostedService
{
	private readonly ILogger Logger = loggerFactory.CreateLogger<ExampleService>();

	private string name = "unknown";
	public async Task<bool> SetNameAsync(string Name)
	{
		this.name = Name;

		Logger.LogInformation("SetNameAsync {Name}", Name);

		await Task.Delay(100);

		return true;
	}

	public async Task<string> GetNameAsync()
	{
		Logger.LogInformation("GetNameAsync {name}", name);

		await Task.Delay(100);

		return name;
	}

	public async Task StartAsync(CancellationToken cancellationToken)
	{
		Logger.LogWarning("StartAsync");

		await Task.Delay(100);
	}

	public async Task StopAsync(CancellationToken cancellationToken)
	{
		Logger.LogWarning("StopAsync");

		await Task.Delay(100);
	}
}
  1. Configure AddSessionScoped

Register the session and SessionScoped in your Startup.cs or Program.cs file:

using SessionScopedTest.Services;

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
	ContentRootPath = AppContext.BaseDirectory
});

var services = builder.Services;

services.AddHttpContextAccessor();
services.AddMvc();

services.AddDistributedMemoryCache();
services.AddSession(options =>
{
	options.IdleTimeout = TimeSpan.FromMinutes(20);
	options.Cookie.Name = ".AspNetCore.Session";
	options.Cookie.HttpOnly = true;
	options.Cookie.IsEssential = true;
});

services.AddSessionScoped<ExampleService>();

var app = builder.Build();

app.UseDefaultFiles();
app.UseStaticFiles();
app.UseSession();
app.MapDefaultControllerRoute();

app.Run();
  1. Use SessionScopedFactory<T>

Inject SessionScopedFactory<ExampleService> factory into your services or controllers to get the instance of your session-scoped service:

public class ExampleController(SessionScopedFactory<ExampleService> factory) : ControllerBase
{
	public async Task<IActionResult> Index()
	{
		HttpContext.Session.SetString("Started", DateTime.Now.ToString());

		var result = await factory.Instance.SetNameAsync($"alphons {Guid.NewGuid()}");

		return Ok("name is set, check url /Example/WhatsYourName");
	}
	public async Task<IActionResult> WhatsYourName()
	{
		var name = await factory.Instance.GetNameAsync();

		return Ok(name);
	}

}

Requirements

  • .NET 9 or higher
  • ASP.NET Core

Contributing

Contributions are welcome! Feel free to fork the repository and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.


For more details, visit the GitHub repository.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
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
9.0.2 65 1/20/2025
9.0.1 70 1/19/2025
9.0.0 67 1/19/2025